1

I am currently making an application where the user can add multiple accounts. The login information is being stored in a text file. I need a way for the user to add a new account without BufferedWriter overwriting the other accounts' information. Any suggestions?

package mainFiles.fileManagers;

import java.io.*; 


public class NewTXT {


 public static void nFile( String a , String b ) throws IOException {

     File f = new File("mainFiles\\storage\\accountinfo.txt");
     FileWriter writer = new  FileWriter("mainFiles\\storage\\accountinfo.txt"); 
     BufferedWriter bWriter = new BufferedWriter(writer); 



     f.createNewFile(); 
     f.canWrite(); 
     bWriter.newLine(); 
     bWriter.write(a + "?" + b );
     bWriter.newLine();
     bWriter.close(); 

 }

}
ddjh5
  • 67
  • 1
  • 7

1 Answers1

5

There is nothing wrong with the BufferedWriter. You have to construct the FileWriter in append mode:

FileWriter writer = new FileWriter("mainFiles\\storage\\accountinfo.txt", true);

Check the FileWriter documentation: https://docs.oracle.com/javase/7/docs/api/java/io/FileWriter.html#FileWriter(java.lang.String,%20boolean)

Balázs Nemes
  • 699
  • 4
  • 9