java file.renameTo()重命名文件,但返回false。为什么? [英] java file.renameTo() does rename file but returns false. Why?

查看:3152
本文介绍了java file.renameTo()重命名文件,但返回false。为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

然而,当我在我的逻辑的其余部分工作之前,我需要文件移动,所以当方法返回false时,我停止执行。检查在Windows资源管理器中的文件,它有一个新的名称,它移动。

只是好奇为什么发生这种情况。



这里是一些示例代码,我试图重新创建这个问题。这是几乎相同的事情,它工作正常。

 文件testfile =新文件(TestFile); $!
$ b if(!testfile.exists()){

testfile.mkdirs();

}

File sample = new File(sample.txt); (sample.exists()){

boolean success = sample.renameTo(new File(testfile.getPath()+\\+ sample)。的getName()));

if(success){

System.out.println(Moved);

}
else {

System.out.println(Failed);



$ / code $ / pre
$ b $ p

编辑:已解决它。我很抱歉浪费大家的时间这么傻。然而,我真的不认为我会追查下来,如果不是因为这个职位。



解决方案是,我实际上循环移动几个文件。当输出表示失败,程序停止,当我在资源管理器中查看只有第一个文件被实际移动,所以我认为它正在移动,然后返回false。然而,问题是我正在使用错误的变量作为索引,所以发生了什么事是它成功地移动索引0中的文件,然后当循环重复索引没有增加,所以它试图再次移动索引0和所以失败了。



像我说的,很愚蠢,但是要感谢我的支持。 解决方案< Java的 File.renameTo()是有问题的,特别是在Windows上,似乎。正如API文档所述:


此方法的许多方面的行为本质上是
平台相关的:重命名操作可能不能将
文件从一个文件系统移动到另一个文件系统,它可能不是原子的,如果具有目标抽象路径名
的文件已经存在,
可能不会成功。应该总是检查返回值,以确保
重命名操作成功。

您可以使用apache.commons .io库,其中包括 FileUtils.moveFile()或JDK 7中的 Files.move()方法。

The problem is that I need the file to move before the rest of my logic will work so when the method returns false I stop execution.

However, when I check on the file in windows explorer it has a new name and it moved.

Just curious why this is happening.

here is some sample code I just tried to recreate the issue. It's pretty much the same thing and it's working fine.

File testfile = new File("TestFile");

    if(!testfile.exists()){

        testfile.mkdirs();

    }

    File sample = new File("sample.txt");

    if(sample.exists()){

        boolean success = sample.renameTo(new File(testfile.getPath() + "\\" + sample.getName()));

        if(success){

            System.out.println("Moved");

        }
        else{

            System.out.println("Failed");

        }

    }

Edit: Solved it. I'm sorry for wasting everyone's time with something so silly. However, I really dont think I would have tracked this down if not for making this post.

The solution was that I was actually looping through several files to move. When the output said it failed then the program stopped and when I looked in explorer only the first of the files was actually moved so I assumed it was moving and then returning false. However, the issue was that I was using the wrong variable as an index and so what was happeneing was that it did successfully move the file in index 0 and then when the loop repeated the index didnt increment so it tried to move index 0 again and therefore failed.

Like I said, very stupid but thanks for bearing with me.

解决方案

Java's File.renameTo() is problematic, especially on Windows, it seems. As the API documentation says:

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.

You can use apache.commons.io library, which includes FileUtils.moveFile() or also the Files.move() method in JDK 7.

这篇关于java file.renameTo()重命名文件,但返回false。为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆