I posted an earlier question asking about how to write to a CSV file, and I got lots of support but unfortunately every time the code runs it overwrites the old data, but I need to add to the data of the file not to overwrite it.
the application is a simple login page that stores the username and password injected by the user inside a CSV file.
public void setCreateButton() throws Exception {
String newUsername = usernameTextField.getText();
String newUserPassword = passwordField.getText();
String confirmedPassword = confirmPasswordField.getText();
try {
if (newUserPassword.equals(confirmedPassword)) {
File file = new File("D:\\Users\\login.csv");
PrintWriter printWriter = new PrintWriter(file);
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append(newUsername);
stringBuilder.append(',');
stringBuilder.append(newUserPassword);
stringBuilder.append('\n');
printWriter.write(stringBuilder.toString());
printWriter.flush();
printWriter.close();
} else {
System.out.println("Password are not matched");
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
Every time the button is clicked, it overwrites the data in the CSV file, I just need something to skip any line that is existing in the file and add a new line with the new data.