0

I have a text file including name, card number, balance and PIN. When I want to create a new account, it overwrites the existing file, which I don't want. If I create 2 new accounts at the same time, by adding another account, it will be replaced by the very first one and if I run it by one of my methods, it will double the existing account which the operation has been done on. Can someone please explain to me what should I do? pic pic

I have commented the print writer-part from existing code and it was no use.

The problem starts where I want this to be able to append a new line and update the existing data at the same time.(updating balance for example)

Rojin
  • 1,045
  • 9
  • 15

1 Answers1

1

Well you can save the old data and just re-write it to the file, or you can append the new data to the file.

BufferedWriter writer = new BufferedWriter(new FileWriter(file, true));

By setting the parameter to true in FileWriter it now will append new data to the file instead of overwriting it.

Julian Peffer
  • 307
  • 1
  • 4
  • This is the way I have written...but everything I do on this file will create another line of the same person with a difference in balance and if I want to run deposit() one more time, (I think because of my index) I can't. – Rojin Apr 01 '19 at 15:10
  • So you are saying it re-adds the same data back into the file? Like duplicates? – Julian Peffer Apr 01 '19 at 15:11
  • Well when you open the FileWriter and PrintWriter you are clearing the file, is this intended? – Julian Peffer Apr 01 '19 at 15:14
  • yes but when I want to create a new account it will be replaced by the very first one – Rojin Apr 01 '19 at 16:15