2014-01-28 44 views
0

我正在編寫一個程序,該程序可以讀取兩個文本文件並找到差異 但由於某種原因,我無法打印結果集。我檢查了很多次,仍然找不到原因,我希望你們能幫助我。這裏是代碼。無法在Java中打印出一套

問題發生在每個打印設置。

import java.io.*; 
    import java.util.*; 

    public class PartOne { 


    public static void readFileAtPath(String filePathOne, String filePathTwo) { 
     // Lets make sure the file path is not empty or null 
     if (filePathOne == null || filePathOne.isEmpty()) { 
      System.out.println("Invalid File Path"); 
      return; 
     } 

     if (filePathTwo == null || filePathTwo.isEmpty()) { 
      System.out.println("Invalid File Path"); 
      return; 
     } 

     Set<String> newUser = new HashSet<String>(); 
     Set<String> oldUser = new HashSet<String>(); 

     BufferedReader inputStream = null; 
     BufferedReader inputStream2 = null; 
     // We need a try catch block so we can handle any potential IO errors 
     try { 
      // Try block so we can use ‘finally’ and close BufferedReader 
      try { 
       inputStream = new BufferedReader(new FileReader(filePathOne)); 
       inputStream2 = new BufferedReader(new FileReader(filePathTwo)); 

       String lineContent = null; 
       String lineContent2 = null; 

       // Loop will iterate over each line within the file. 
       // It will stop when no new lines are found. 
       while ((lineContent = inputStream.readLine()) != null) { 
        // Here we have the content of each line. 
        // For now, I will print the content of the line. 
        // System.out.println("Found the line: " + lineContent); 
        oldUser.add(lineContent); 
       } 

       while ((lineContent2 = inputStream.readLine()) != null) { 
        newUser.add(lineContent2); 
       } 

       Set<String> uniqueUsers = new HashSet<String>(newUser); 
       uniqueUsers.removeAll(oldUser); 

      } 
      // Make sure we close the buffered reader. 
      finally { 
       if (inputStream != null) 
        inputStream.close(); 
       if (inputStream2 != null) 
        inputStream2.close(); 
      } 


      for (String temp : uniqueUsers) { 
       System.out.println(temp); 
      } 

     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    }// end of method 

    public static void main(String[] args) { 
     String filePath2 = "userListNew.txt"; 
     String filePath = "userListOld.txt"; 
     readFileAtPath(filePath, filePath2); 

    } 
} 
+1

嘗試調試應用程序。 –

+0

錯誤說的是什麼? – Prince

回答

5

你的問題是範圍。你在你的try塊中定義了這個集合,但是你試圖從該塊之外訪問它。您必須在想要使用該變量的同一範圍內定義所有變量。

將uniqueUsers的定義移動到try塊之前。

*編輯回覆您的評論。

您正在從相同的輸入流中讀取兩次。第二個while循環應該從inputStream2讀取。

+0

+1好抓! :D – Prince

+0

謝謝!現在我有另一個問題。它已經成功地執行並運行。但它的打印什麼都沒有它應該打印出兩套的差異。 – Samson

+0

@Samson我更新了我的答案 – Dodd10x

0

試試這個

try { 
    // Try block so we can use ‘finally’ and close BufferedReader 
    try { 
     inputStream = new BufferedReader(new FileReader(filePathOne)); 
     inputStream2 = new BufferedReader(new FileReader(filePathTwo)); 

     String lineContent = null; 
     String lineContent2 = null; 

     // Loop will iterate over each line within the file. 
     // It will stop when no new lines are found. 
     while ((lineContent = inputStream.readLine()) != null) { 
      // Here we have the content of each line. 
      // For now, I will print the content of the line. 
      // System.out.println("Found the line: " + lineContent); 
      oldUser.add(lineContent); 
     } 

     while ((lineContent2 = inputStream.readLine()) != null) { 
      newUser.add(lineContent2); 
     } 

     Set<String> uniqueUsers = new HashSet<String>(newUser); 
     uniqueUsers.removeAll(oldUser); 

     for (String temp : uniqueUsers) { 
     System.out.println(temp); 
     } 

    } 
    // Make sure we close the buffered reader. 
    finally { 
     if (inputStream != null) 
      inputStream.close(); 
     if (inputStream2 != null) 
      inputStream2.close(); 
    }   

} catch (IOException e) { 
    e.printStackTrace(); 
}