So I'm trying to store a list of users into an external text file, and If they dont have an account already, then they need to sign up, and their account gets added to the text file.
However each time I create a new user, it overwrites the last one in the text file.
Can anyone see what wrong with my code for creating a new user, and if theres something obvious that would fix it?
EDIT:
I believe that the text file is being recreated every time that I run the program, how can I just add to it, instead of making a new one each time?
System.out.println("Enter your full name below (e.g. John M. Smith): ");
String name = scanner.nextLine();
System.out.println("Create a username: ");
String userName = scanner.nextLine();
System.out.println("Enter your starting deposit amount: ");
double balance = scanner.nextInt();
System.out.print(dash);
System.out.print("Generating your information...\n");
System.out.print(dash);
int pin = bank.PIN();
String accountNum = bank.accountNum();
User user = new User(name, userName, pin, accountNum, balance);
// new user gets added to the array list
Bank.users.add(user);
System.out.println(user);
}
try {
File file = new File("users.text");
if (!file.exists()) {
file.createNewFile();
}
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.append(String.valueOf(Bank.users));
bw.close();
System.out.print("DONE");
} catch (IOException e) {
e.printStackTrace();
}