I am using netbeans windows 11, I just want to save input entered to a text file without it getting overwritten ideally. It works fine on the first run, text file gets created, input gets written to the file, but any entries after that don't get saved and neither does the file get overwritten with the new entry.
if (!newUsername.getText().equals("") && (!newPass.getText().equals(""))){
// Check to see if the Username.txt file already exists
File file = new File("Username.txt");
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
// Handle the exception
e.printStackTrace();
}
FileWriter fileWriter = null;
try {
fileWriter = new FileWriter(file, true);
// Write the newUsername variable information to the text file
fileWriter.write(newUsername.getText());
} catch (IOException e) {
// Handle the exception
e.printStackTrace();
} finally {
// Close the FileWriter object
if (fileWriter != null) {
try {
fileWriter.close();
} catch (IOException e) {
// Handle the exception
e.printStackTrace();
}
}
}
}
new Splash().setVisible(true);
System.out.println(System.getProperty("user.dir"));
dispose();
}else {
feedback.setText("Fields can't be blank");
}
}
I didn't try anything else I mean I asked google Bard, it said it might be a file permissions issue but that doesnt make sense because like I said it does work fine on the first run, file gets created gets written as expected, so can't be a file permissions issue i think.