到目前为止我所拥有的:
一段代码,用于输入用户名和密码并将其写入文本文件。
String usernameFilename;
usernameFilename = newUsernameField.getText();
char[] signupPassword = newPasswordField.getPassword();
String writePassword = new String(signupPassword);
try {
FileWriter userInfoWriter = new FileWriter(usernameFilename);
BufferedWriter writeToFile = new BufferedWriter(userInfoWriter);
writeToFile.write(usernameFilename);
writeToFile.write("\r\n" + writePassword);
writeToFile.close();
我需要完成的任务:
users
的预制文件夹的目录。 usernameFilename
目录。我尝试了什么:
额外信息:
由于所有计算机都不同,我想在创建目录时使用.getAbsolutePath()
方法。
答案 0 :(得分:0)
看看:
你可以更新你的代码,看看这个......
String usernameFilename;
usernameFilename = newUsernameField.getText();
char[] signupPassword = newPasswordField.getPassword();
String writePassword = new String(signupPassword);
File users = new File("users");
if ((users.exists() && users.isDirectory()) || users.mkdirs()) {
FileWriter userInfoWriter = null;
BufferedWriter writeToFile = null;
try {
userInfoWriter = new FileWriter(users.getPath() + File.seperator + usernameFilename);
writeToFile = new BufferedWriter(userInfoWriter);
writeToFile.write(usernameFilename);
writeToFile.newLine();
writeToFile.write(writePassword);
//....
} finally {
try {
writeToFile.close();
} catch (Exception exp) {
}
}
} else {
throw new IOException("Could not create/find Users directory");
}