-
Saving to a Text File
What is the Code if yur trying to save and want to save to the same text [B]WITHOUT replacing the existing text. Instead the next goes to next line.
here the code Im using:
if(event.getSource() == signupButton) {
try {
outFile1 = new PrintWriter ( new BufferedWriter( new FileWriter("register.txt")));
outFile1.print(usernameField.getText()+", ");
outFile1.print(password.getText()+", ");
outFile1.close();
JOptionPane.showMessageDialog(null,"Data have been Saved");
}
-
If you want to "APPEND" your text to the end of an existing file, read the API doc for the file class and look for an arg that says to append to that file.
-
Read more about FileWriter class on the API.
Code:
outFile1 = new PrintWriter ( new BufferedWriter( new FileWriter("register.txt", true)));
I wonder why are you using such an annoying way to write data to a file. Seems to me you no need to use PrintWriter instance there.