java简单登录系统用户删除帐户文件系统异常

时间:2018-04-12 13:30:18

标签: java file login ioexception

您好我正在尝试为作业实施简单的用户登录系统,这意味着可以删除您的帐户。在启动时,用户登录系统程序在目录中查找文件夹,每个文件夹名称代表不同的用户,每个用户文件夹包含一个“INFO.txt”文件,其中包含用户名和密码。出于某种原因,当我尝试删除用户目录中的文件时,我从eclipse控制台收到以下错误:

java.nio.file.FileSystemException: users\george\INFO.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: users\george
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;

    }

}

如果有人对我出错的地方有任何指示,我将不胜感激,谢谢

1 个答案:

答案 0 :(得分:0)

可能是您在某些应用程序中打开了“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();
            }
    }
}

这与你的基本相同,但非常简化。请在您的文件系统上创建一个单独的类和一个测试文件并进行测试。如果它工作,你可以确定代码很可能是好的,问题是在环境中。再一次,我猜你已经把它打开了。希望它有所帮助。