运行我的程序后,该方法似乎没有工作(删除并重命名为不工作)。 “tempuserlist”文件已创建,但未重命名为“userlist”。 “用户列表”未删除,也不是“userFile”。我查看过有关类似问题的多篇帖子但没有成功。我使用了import java.io.File;
static void DeleteUser (String[] username) throws IOException
{
BufferedReader stdin = new BufferedReader (new InputStreamReader (System.in));
String response;
System.out.print ("Are you sure you would like to delete this account/username (y/n)? ");
response = stdin.readLine ();
if (response.equals ("y") || response.equals ("Y") || response.equals ("yes") || response.equals ("Yes"))
{
BufferedReader reader = new BufferedReader (new FileReader ("G:\\MP3Folder\\userlist.txt"));
BufferedWriter writer = new BufferedWriter (new BufferedWriter (new FileWriter ("G:\\MP3Folder\\tempuserlist.txt")));
String line = null;
while ((line = reader.readLine ()) != null)
{
if (username [0].equals (line))
{
continue;
}
writer.write (line);
writer.newLine ();
}
writer.close ();
reader.close ();
File userFile = new File ("G:\\MP3Folder\\" + username [0] + ".txt");
boolean success2 = userFile.delete ();
File n = new File ("G:\\MP3Folder\\userlist.txt");
boolean success1 = n.delete ();
File o = new File ("G:\\MP3Folder\\tempuserlist.txt");
boolean success = o.renameTo (n);
System.out.println ("\nUser deleted.");
}
else
{
System.out.println ("\nAccount deletion cancelled.");
return;
}
}
答案 0 :(得分:0)
您拥有的代码是正确的。更改路径后,相同的代码在我的环境中正常工作。您需要将路径从G:更改为另一个本地目录,例如C:或D:
答案 1 :(得分:0)
整个 DeleteUser()方法很可疑。我不明白你为什么要传递一个字符串数组(用户名),它显然只被编码为删除一个用户文件,而用户将是字符串的第一个元素中包含的用户数组(除非您当然没有告诉我们所有内容)。方法声明应为:
private static void deleteUser(String username) { .......... }
在任何情况下......另一件事,我们如何知道 username.txt 文件甚至存在于:
File userFile = new File("G:\\MP3Folder\\" + username[0] + ".txt");
boolean success2 = userFile.delete ();
如果没有,则抛出 FileNotFound Exception ,剩余的代码将无法播放。你或许应该:
File userFile = new File("G:\\MP3Folder\\" + username[0] + ".txt");
if (userFile.exists()) { userFile.delete(); }
没有意义声明布尔变量(例如成功, success1 和 success2 ),因为无论如何都不会对它们做任何事情。< / p>
以下是我将如何创建此方法,但又一次......每个人都做了一些不同的事情:
private static void deleteUser(String userName) {
String[] TheButtons = {"YES - Delete","NO - Don't Delete"};
int res = JOptionPane.showOptionDialog (null, "Are you sure you would like to delete the account/username\n"
+ "titled: " + userName, "Delete User Account", JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE, null, TheButtons, TheButtons[1]);
// NO selected
if (res == 1) {
System.out.println ("\nAccount Deletion Cancelled.");
return;
}
try {
try (BufferedReader reader = new BufferedReader (new FileReader("G:\\MP3Folder\\userlist.txt"));
BufferedWriter writer = new BufferedWriter (new BufferedWriter(new FileWriter("G:\\MP3Folder\\tempuserlist.txt")))) {
String line = null;
while ((line = reader.readLine ()) != null) {
if (userName.equals(line)) { continue; }
writer.write (line);
writer.newLine ();
}
}
File userFile = new File ("G:\\MP3Folder\\" + userName + ".txt");
if (userFile.exists()) { userFile.delete(); }
File n = new File ("G:\\MP3Folder\\userlist.txt");
File o = new File ("G:\\MP3Folder\\tempuserlist.txt");
if (n.exists() && o.exists()) {
n.delete();
o.renameTo(n);
System.out.println ("\nUser Deleted!");
}
else {
System.out.println ("\nError - File Not Found! - User Not Deleted!");
}
}
catch (IOException ex) {
System.out.println ("\nIO Exception Encountered!\n\n" + ex.getMessage());
}
}
答案 2 :(得分:0)
大家好,感谢那些有直接支持和建议的人。我发现了一个在初始化后没有关闭的BufferReader和BufferWriter,我修复了我的问题。程序仍然使用username.txt,我无法删除或重命名文件。
@DevilsHnd我第一次有一个用户名数组,因为这个方法只是我计算机科学最终作业的一小部分(600行代码),我需要其他方法的数组。我尝试使用布尔值随机原因,希望它可以工作。如果它存在,我不需要对“username.txt”进行任何验证,因为如果我的程序首先运行它必须存在。对于缺乏洞察力和背景感到抱歉,但感谢您的帮助。
@Crusauderpyro感谢您亲自试用代码。虽然我使用的是外部驱动器(USB),但问题并不存在,因为它现在已成功运行。谢谢你的帮助。