您好,我正在尝试为作业实现一个简单的用户登录系统,这意味着能够删除您的帐户。启动时,用户登录系统程序在目录中查找文件夹,每个文件夹名称代表不同的用户,每个用户文件夹都包含一个"INFO.txt"文件,该文件显示其用户名和密码。出于某种原因,当我尝试删除用户目录中的文件时,我从 eclipse 控制台收到以下错误:
java.nio.file.FileSystemException: usersgeorgeINFO.txt: The process cannot access the file because it is being used by another process.
at sun.nio.fs.WindowsException.translateToIOException(Unknown Source)
at sun.nio.fs.WindowsException.rethrowAsIOException(Unknown Source)
at sun.nio.fs.WindowsException.rethrowAsIOException(Unknown Source)
at sun.nio.fs.WindowsFileSystemProvider.implDelete(Unknown Source)
at sun.nio.fs.AbstractFileSystemProvider.delete(Unknown Source)
at java.nio.file.Files.delete(Unknown Source)
at MessagingApp.deleteAccount(MessagingApp.java:414)
at MessagingApp.mainMenu(MessagingApp.java:333)
at MessagingApp.login(MessagingApp.java:237)
at MessagingApp.start(MessagingApp.java:180)
at MessagingApp.<init>(MessagingApp.java:30)
at Main.main(Main.java:5)
java.nio.file.DirectoryNotEmptyException: usersgeorge
at sun.nio.fs.WindowsFileSystemProvider.implDelete(Unknown Source)
at sun.nio.fs.AbstractFileSystemProvider.delete(Unknown Source)
at java.nio.file.Files.delete(Unknown Source)
at MessagingApp.deleteAccount(MessagingApp.java:424)
at MessagingApp.mainMenu(MessagingApp.java:333)
at MessagingApp.login(MessagingApp.java:237)
at MessagingApp.start(MessagingApp.java:180)
at MessagingApp.<init>(MessagingApp.java:30)
at Main.main(Main.java:5)
这是我的类,其中包含我的删除帐户方法和启动时调用的加载方法:
public class MessagingApp {
private Scanner scanner;
private ArrayList<User> userList; //All users currently registered
private User currentUser; //The user currently logged in
public MessagingApp() {
scanner = new Scanner(System.in);
userList = new ArrayList<User>();
try {
this.load();
} catch (IOException e) {
e.printStackTrace();
}
this.start();
}
/**
* loads all user information currently stored
* @throws IOException
*/
private void load() throws IOException {
Path users = Paths.get("users");
File usersDir = users.toFile();
String[] entries = usersDir.list(); //Each string in this list represents a user folder
if(entries!=null) {
for(String s: entries) {
Path thisUser = Paths.get("users/"+s);
File thisUserDir = thisUser.toFile();
String[] userFiles = thisUserDir.list();
for(String file: userFiles) {
if( file.equals("INFO.txt") ) {
try {
BufferedReader reader = new BufferedReader(new FileReader("users/"+s+"/INFO.txt"));
String info = reader.readLine();
String[] credentials = info.split(":");
userList.add(new User(credentials[0],credentials[1]));
credentials=null;
info = null;
reader=null;
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
}
}
}
private void deleteAccount() {
String choice;
System.out.println("Are you sure you want to delete your account?");
while(true) {
choice = scanner.nextLine();
if((choice.equals("y"))||(choice.equals("n"))) {
break;
}
System.err.println("Invalid selection");
}
if( choice.equals("n") ) {
mainMenu();
}
if( choice.equals("y") ) {
Path userPath = Paths.get("users/"+currentUser.getUsername());
File userDir = userPath.toFile();
String[] userFiles = userDir.list();
if(userFiles!=null) {
for(String s: userFiles) {
Path userFilePath = Paths.get("users/"+currentUser.getUsername()+"/"+s);
try {
Files.delete(userFilePath);
} catch (IOException e) {
e.printStackTrace();
}
}
}
try {
Files.delete(userPath);
} catch (IOException e) {
e.printStackTrace();
}
userList.remove(currentUser);
currentUser=null;
}
}
如果有人有任何关于我出错的地方的指示,将不胜感激,谢谢
您在某个应用程序中打开了"users\george\INFO.txt"。例如,在记事本或 IDE 中。因此,它会锁定它,使其不被您的应用删除。
我给你的建议是单独尝试这个特定的功能。例如,我已经尝试了以下代码片段,它适用于我的环境。
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class MessagingApp {
public static void main(String args[]) {
deleteAccount();
}
private static void deleteAccount() {
Path userFilePath = Paths.get("Z:/users/test/INFO.txt");
try {
Files.delete(userFilePath);
} catch (IOException e) {
e.printStackTrace();
}
}
}
这基本上与您相同,但非常简化。请在文件系统上创建一个单独的类和一个测试文件并对其进行测试。如果它有效,您可以确定代码很可能是正常的,并且问题出在环境中。再说一次,我猜你在其他地方打开了它。希望对您有所帮助。