0

I'm coding a simple application with a login page, I created a page for creating a new user, it should ask for the Username once and the Password twice, the injected data should be stored in an empty CSV file that already exists... I searched the internet and this is what I came up with.

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());
        } else {
            System.out.println("Password are not matched");
        }
    } catch (Exception e) {
        System.out.println(e.getMessage());
    }
}

when I run the code it doesn't generate any error but also it makes no changes to the CSV file, of course, I'm doing something wrong but I just can't figure it out.

4

1 回答 1

0

You need to flush the writer:

printWriter.flush();
于 2020-02-07T13:40:41.533 回答