John
John

Reputation: 177

Is my write text to file wrong?

I have a file call "CI.txt"

Inside the file the information is:

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

Here is my code, it works very well but the problem is..

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");
     }                                     
}

My output will only have the records of Mr Lmn. There is no record of Mr Abc whom I updated the new credit card type and number. Why is this happening? I did System.out.println(text) in the try statement and all was printed out properly. Anyone can help?

Upvotes: 1

Views: 113

Answers (6)

W.K.S
W.K.S

Reputation: 10095

The problem is that you are writing to the file inside the for loop. This means that with each loop, the file is overwritten with new data. In the end, only the last data is shown. You need to move the for-loop code inside the file-writing code, like this:

    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" );
        }

Upvotes: 1

CloudyMarble
CloudyMarble

Reputation: 37566

You are building the Text and creating new file for each customer, so the last one override all the others:

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);
     //...

}

You need to create the file outside the loop, then build the content and fill the file with data, and finally close the file.

Upvotes: 2

AllTooSir
AllTooSir

Reputation: 49372

Use :

public FileWriter(File file,boolean append)
       throws IOException

It says , append - if true, then bytes will be written to the end of the file rather than the beginning

Here is the API doc.

Upvotes: 0

Boistyjones
Boistyjones

Reputation: 19

You are running each customer in a loop.

for (Customer ci : custList){

Each time you run the loop, you create a new file called CI.txt

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

Since you create the file from scratch for every customer, only the last customer will remain. Open the file for append instead.

Upvotes: 0

mishadoff
mishadoff

Reputation: 10789

The problem in your code that each for-loop iteration recreates the file and overwrites its content

Upvotes: 2

Marko Topolnik
Marko Topolnik

Reputation: 200148

You are opening and closing the file in each iteration of the for-loop. Opening a file by default erases everything in it. You must open the file before starting the for-loop, and close it only afterwards.

Upvotes: 5

Related Questions