I'm trying to create a program which saves text on a file and then text can be added onto the file. However, every time i try to write to the file, it overwrites it and doesn't write anything. I need it to add whatever information i want it UNDER the rest.
FileReader input;
BufferedReader readFile;
FileWriter output;
BufferedWriter writeFile;
try {
// input = new FileReader(password_file);
//readFile = new BufferedReader(input);
output = new FileWriter(password_file);
writeFile = new BufferedWriter(output);
//while ((temp_user= readFile.readLine()) !=null) {
//temp_pass = readFile.readLine();
//}
temp_user = save_prompt.getText();
temp_pass = final_password;
//Writes to the file
writeFile.write(temp_user);
writeFile.newLine();
writeFile.write(temp_pass);
}
catch(IOException e) {
System.err.println("Error: " + e.getMessage());
}
}
What you seek for is Append mode.
new FileWriter(file,true); // true = append, false = overwrite
Replace all existing content with new content.
new FileWriter(file);
Keep the existing content and append the new content in the end of the file.
new FileWriter(file,true);
Example:
FileWriter fileWritter = new FileWriter(file.getName(),true);
BufferedWriter bufferWritter = new BufferedWriter(fileWritter);
bufferWritter.write(data);
bufferWritter.close();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With