2013-04-29 71 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