java - Login Page and writing data to CSV file
问题描述
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.
解决方案
You need to flush the writer:
printWriter.flush();
推荐阅读
- html - 硬证明内容
- maven - Maven 依赖 - 问题
- c# - asp .net 中的第二个布局中的 RenderBody 错误
- javascript - 试图在 localhost 中为已经运行的服务器捕获客户端 IP 地址,但 node.js 与现有服务器冲突
- javascript - 使用 Immutable JS 和 Redux - 动作未定义
- php - 为什么在 Laravel 的控制器中没有正确重定向 if else ?
- material-ui - 有没有一种简单的方法可以在 react-autosuggest 项目中使用主题颜色
- sql - 用前一行sql server更新当前行
- javascript - 为什么 AuthGuard 中的值不会更改为 false?
- regex - Windows 命令行组策略 - 使用 findstr/regex