我刚学会了创建和阅读文件,我正在通过制作一个小程序来练习它。问题是我的构造函数无法创建我需要的文件。
这是我的主要内容:
public static void main(String[] args) {
createUser user = new createUser("username", "password");
}
这是我的班级:
public class createUser {
private Formatter formatter;
private Scanner scanner;
private File file;
private String username;
private String password;
createUser(String username, String password){
this.username = username;
this.password = password;
try{
String path = "D:\\Users\\" + this.username +".txt";
file = new File(path);
formatter = new Formatter(path);
scanner = new Scanner(new File(path));
formatter.format("Username: %s\n", username);
formatter.format("Password: %s\n", password);
formatter.close();
System.out.printf("%s has been created!", this.username);
}
catch(Exception e){
System.out.println("Failed to create the account!");
}
}
}
输出: 无法创建帐户!
答案 0 :(得分:0)
事实证明,我必须手动创建用户文件夹。