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

问题描述

我正在尝试创建一种身份验证系统,该系统使用名为 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,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笔记本电脑上仍然看到相同的问题。

解决方法

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

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...