java - File.renameTo() 失败,但在创建 FileOutputStream 对象后可以工作,为什么?

标签 java file

[编辑]我无法使用 renameTo() 重命名我的文件方法File类(class)。好吧,我搜索并发现了一个解释相同问题的问题

File.renameTo() fails?

我还阅读了 renameTo() 的 Java 文档其中说:

Many aspects of the behavior of this method are inherently platform-dependent: The rename operation might not be able to move a file from one filesystem to another, it might not be atomic, and it might not succeed if a file with the destination abstract pathname already exists. The return value should always be checked to make sure that the rename operation was successful.

好的,我明白了 renameTo()方法依赖于平台。

然后我创建了FileOutputStream的对象并调用close()方法,现在我尝试过renameTo()方法和我的文件被重命名,

问题:

  • 我无法理解创建 FileOutputStream 对象后的原因我的renameTo()方法有效吗?

环境: Windows XP,用户:管理员

代码:

    File f = null;
    File f1 = null;
    boolean isFileRenamed = false;

    try {
        // create new File objects
        f = new File("C:\\originalFile.txt");
        f1 = new File("C:\\renamedFile.txt");

        // I need to write following code to rename the file
        // I tried without FileOutputStram object but then renameTo() did not work
        FileOutputStream fos = new FileOutputStream(f);
        fos.close();

        isFileRenamed = f.renameTo(f1);
        System.out.print("File renamed? " + isFileRenamed);

    } catch (Exception e) {
        e.printStackTrace();
    }

正在寻找为什么 renameTo() 方法在创建 FileOutputStram 对象后起作用的答案。另外,我的应用程序使用 Java1.6,所以我的选择是 Files类(class)已关闭。我将不得不使用renameTo()仅方法

最佳答案

这不是 JDK 中的错误。来自 the documentation :

Many aspects of the behavior of this method are inherently platform-dependent: The rename operation might not be able to move a file from one filesystem to another, it might not be atomic, and it might not succeed if a file with the destination abstract pathname already exists. The return value should always be checked to make sure that the rename operation was successful.

(我的重点)

不同的驱动器是不同的文件系统。在文件系统之间移动文件需要将文件的数据从旧文件系统复制到新文件系统,并在复制完成后删除其原始位置的文件。 renameTo 不承担这项工作,它适用于简单的情况,即文件可以在文件系统中移动

<小时/>

你在下面说它甚至不能在文件系统中工作。请注意,如果您使用的是 Windows 7,则必须以管理员身份运行才能在 C: 驱动器的根目录中创建文件。

如果我以管理员身份在命令提示符下运行,这将起作用:

示例代码:

import java.io.*;

public class FileMove
{
    public static final void main(String[] args) {
        File f = null;
        File f1 = null;
        boolean bool = false;

        try {
            // create new File objects
            f = new File("C:\\test.txt");
            f1 = new File("C:\\renamed.txt");

            bool = f.renameTo(f1);

            System.out.print("File renamed? " + bool);

        } catch (Exception e) {
            // if any error occurs
            e.printStackTrace();
        }
    }
}

运行示例:

C:\>echo "Testing 1 2 3">test.txt
C:\>type c:\test.txt
"Testing 1 2 3"
C:\>type c:\renamed.txt
The system cannot find the file specified.
C:\>java -cp . FileMove
File renamed? true
C:\>type renamed.txt
"Testing 1 2 3"
C:\>

但正如 millimoose 在评论中指出的那样,有 Files.Move method .

关于java - File.renameTo() 失败,但在创建 FileOutputStream 对象后可以工作,为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17900500/

相关文章:

java - 从另一个 Spring Boot JPA 项目插入的数据库中读取数据

java - 在android studio中获取连接被拒绝(连接被拒绝)

javascript - 刷新问题?脚本在每次刷新时继续运行

java - 在 Android Studio 中加载文件

java - 哪种 toString 技术更有效?

java - AspectJ - 在运行时使用自定义 ClassLoader 编织

java - WebSocket 握手期间出错(意外响应代码 : 400)

python - 在python中读取一个pgm文件

c++ - 将缓冲区写入文件(存储 1585 字节而不是 1580)

file - 什么时候文件不是文件系统对象?