2013-04-29 73 views
1

我有一個文件叫「CI.txt」我寫的文件是否寫錯了?

文件裏面的信息是:

Mr Abc;ABC;abc123;Abc Road;428428;VISA;2222111144442222 
Mr Efg;EFG;efg123;Efg Road;424213;MASTERCARD;4444555566667777 
Mr Lmn;LMN;lmn123;Lmn Road;492482;VISA;9999000011112222 

這裏是我的代碼,它工作得很好,但問題是..

for (Customer ci : custList){ 
//Compares the username and userpassword 
//If correct, set new card number and card type.. 
if (inputUser.equals(ci.getUserName()) && inputPass.equals(ci.getPassword())) { 
    ci.setCardNo(newCardNo); 
    ci.setCardType(newCardType); 
} 

    String text = ci.getRealName() + ";" + ci.getUserName() + ";" + ci.getPassword() + ";" + ci.getContact() + ";" + ci.getcardType() + ";" + ci.getcardNo(); 
    try { 
     File fileCI = new File("CI.txt"); 
     FileWriter fileWriter = new FileWriter(fileCI); 
     BufferedWriter bw = new BufferedWriter(fileWriter); 
     bw.write(text); 
     bw.close(); 
    } 
    catch (FileNotFoundException e) { 
    System.out.println("File not found"); 
    } 
    catch (IOException e) { 
    System.out.println("Unable to write to file"); 
    }          
} 

我的輸出將只有Lmn先生的記錄。沒有Abc先生的記錄,我更新了新的信用卡類型和號碼。這是爲什麼發生?我在try語句中做了System.out.println(text),並且都正確地打印出來了。任何人都可以幫忙

+2

你寫了一個未加密的信用卡號的純文本文件? – LittleBobbyTables 2013-04-29 11:45:39

+2

@LittleBobbyTables看看信用卡號碼,我認爲OP是學習文件處理,這只是一個測試項目 – Apurv 2013-04-29 11:47:01

+0

是它的測試項目。沒有其他的。爲什麼那麼認真? – John 2013-04-29 11:47:37

回答

1

問題是您正在寫入for循環中的文件。這意味着在每個循環中,文件都會被新數據覆蓋。最後,只顯示最後的數據。你需要移動的循環代碼的文件寫入代碼中,像這樣:

try 
     { 
      File fileCI = new File ("CI.txt"); 
      FileWriter fileWriter = new FileWriter (fileCI); 
      BufferedWriter bw = new BufferedWriter (fileWriter); 

      for (Customer ci : custList) 
      { 
       if (inputUser.equals (ci.getUserName()) 
         && inputPass.equals (ci.getPassword())) 
       { 
        ci.setCardNo (newCardNo); 
        ci.setCardType (newCardType); 
       } 
       String text = ci.getRealName() + ";" + ci.getUserName() + ";" 
         + ci.getPassword() + ";" + ci.getContact() + ";" 
         + ci.getcardType() + ";" + ci.getcardNo(); 

       bw.write (text); 

      } 
bw.close(); 
fileWriter.close(); 

     } 
     catch (FileNotFoundException e) 
     { 
      System.out.println ("File not found"); 
     } 
     catch (IOException e) 
     { 
      System.out.println ("Unable to write to file"); 
     } 
+0

提示:最好在finally塊中關閉BufferedWriter和FileWriter。 – 2013-04-29 11:53:58

+0

感謝您的幫助。我現在明白我的錯誤 – John 2013-04-29 11:54:27

+0

提示:不需要分別捕捉幾個例外。事實上,它比沒有捕獲任何東西更糟糕,因爲現在一些異常將被吞噬,整個方法成功返回,而其他異常會導致該方法拋出異常。這是一個異常處理反模式的典型例子。 – 2013-04-29 11:57:08

2

正在構建的文本,併爲每個客戶創建新文件,所以最後一個覆蓋所有其他:

for (Customer ci : custList){ 
    //... 
    String text = ci.getRealName() + ";" + ci.getUserName() + ";" + ci.getPassword() + ";" +  ci.getContact() + ";" + ci.getcardType() + ";" + ci.getcardNo(); 
    try { 
    File fileCI = new File("CI.txt"); 
    FileWriter fileWriter = new FileWriter(fileCI); 
    //... 

} 

您需要創建,循環外的文件,然後生成的內容和用數據填充文件,最後關閉文件。

+0

謝謝我得到你所說的 – John 2013-04-29 11:49:21

5

您正在打開和關閉for循環的每次迭代中的文件。默認情況下打開文件會擦除其中的所有內容。在開始for循環之前,您必須先打開文件,然後才關閉它。

+0

感謝您的幫助!我做了這麼一個愚蠢的錯誤 – John 2013-04-29 11:54:02

2

在你的代碼的問題,每for循環迭代重新創建文件並覆蓋其內容

+0

我現在不瞭解它。感謝您的幫助 – John 2013-04-29 11:49:47

0

你正在運行在一個循環中每一位客戶。

for (Customer ci : custList){ 

每次運行循環時,將創建一個名爲CI.txt

File fileCI = new File("CI.txt"); 

既然你從頭開始創建該文件對每一位客戶的新文件,只有最後顧客將保持不變。改爲打開文件進行追加。

0

用途:

public FileWriter(File file,boolean append) 
     throws IOException 

它說,追加 - 如果爲true,則將字節寫入文件末尾處,而不是開始

這裏是API doc