File.delete()和File.renameTo()在项目环境中不起作用

时间:2020-10-19 16:07:02

标签: java file project delete-file file-rename

我正在尝试创建一种身份验证系统,该系统使用名为 Users.dat 的文件来存储用户数据。目前,我正在开发一种方法,该方法通过重写 Users.dat文件来删除用户,而忽略指定的用户。下面的代码在基本环境中起作用,其中包含一个包含所有内容的.java文件和同一位置的 Users.dat 文件。删除旧的 Users.dat 文件,并将 Users.dat.tmp 重命名为 User.dat 。 (这里没有问题,一切正常。)

public static boolean RemoveUser(String userName) {
        // TODO remove username from Users.dat
        try {

            File originalFile = new File("Users.dat");
            System.out.println(originalFile.getAbsolutePath());

            BufferedReader read = new BufferedReader(new FileReader("Users.dat"));

            String line = null;
            while ((line = read.readLine()) != null) {
                if (line.indexOf(userName) != -1) {
                    break;
                }
            }
            String[] userInfo = line.split(", ");
            if (!userName.equals(userInfo[2])) {
                System.out.println("Username not found. No users removed.");
                read.close();
                return false;
            }
            File tempFile = new File(originalFile.getAbsolutePath() + ".tmp");
            PrintWriter print = new PrintWriter(new FileWriter(tempFile));
            String lineToRemove = line;

            BufferedReader read2 = new BufferedReader(new FileReader("Users.dat"));
            while ((line = read2.readLine()) != null) {

                if (!line.trim().equals(lineToRemove)) {
                    print.println(line);
                    print.flush();
                }
            }
            print.close();
            read.close();
            read2.close();
            System.out.println(originalFile.getAbsolutePath());
            originalFile.delete(); //This line is not executing correctly
            tempFile.renameTo(originalFile); //Nor is this line

        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        }

        return true;
    }

Users.dat文件格式:

Joe, Last, jlast, 58c536ed8facc2c2a293a18a48e3e120, true
Sam, sone, samsone, 2c2a293a18a48e3e12058c536ed8facc, false
Jane, Best, jbest, 293a18a48e3e12052058c536ed8facc2c, false
Andrew, Estes, Aestes, 63a490d69aa544fd1272a976014ad570, true
Test, User, tuser, 63a490d69aa544fd1272a976014ad570, true

我有两个System.out.println(originalFile.getAbsolutePath())语句,一个在开头,一个在结尾,以确保在所有过程中都不会弄乱路径。

就像我说的那样,代码有效,但是,当我尝试在项目中实现它时,它会创建 Users.dat.tmp 并将正确的数据写入其中,但确实如此不会删除旧的 Users.dat 文件,也不会重命名 Users.dat.tmp 文件来替换 Users.dat 。我确定目录是正确的,因为我在执行代码时会按字面意义显示它。我找不到其他任何原因导致originalFile.delete()和tempFile.renameTo(originalFile)无法正常运行。

编辑: 使用java.nio.file,我能够产生一条错误消息。它显示为:

java.nio.file.FileSystemException: C:\Path\Users.dat: The process cannot access the file because it is being used by another process.

当显示此错误消息时,我没有打开文件,并且在开头提到的测试环境中,使用java.nio也未收到此错误。我不确定消息所指的其他进程。

编辑2: 我尝试在其他计算机(一台Mac,另一台Windows笔记本电脑)上运行代码,并且该代码在Mac上正常运行,但是在Windows笔记本电脑上仍然看到相同的问题。

1 个答案:

答案 0 :(得分:0)

我在main中调用了一个较早的函数,该函数正在访问Users.dat,但是我从未在该函数中关闭BufferredReader。