java File.renameTo() 失败
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13826045/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
File.renameTo() fails
提问by yuris
I have eclipse plugin jface application. A thread writes file via BufferedWriter. After writing is done I close the buffer after that I try to rename the file.
我有 eclipse 插件 jface 应用程序。一个线程通过 BufferedWriter 写入文件。写入完成后,我关闭缓冲区,然后尝试重命名文件。
But sometimes file is not renamed!
但有时文件没有重命名!
I tried to add some Thread.Sleep(BIG_NUMBER) between couple of retries this didn't help.
我试图在几次重试之间添加一些 Thread.Sleep(BIG_NUMBER) 这没有帮助。
It looks like the file getting some kind of lock. (when I kill the jvm I can rename the file).
看起来文件获得了某种锁定。(当我杀死 jvm 时,我可以重命名文件)。
Is there something I can do?
有什么我可以做的吗?
OS: Windows XP, windows 7 JAVA version: 1.5
操作系统:Windows XP、Windows 7 JAVA 版本:1.5
回答by Jeroen Vannevel
File.RenameTo() is platform dependent and relies on a few conditions to be met in order to succesfully rename a file, a better alternative is using
File.RenameTo() 依赖于平台,并且依赖于满足一些条件才能成功重命名文件,更好的选择是使用
Path source = currentFile.toPath();
try {
Files.move(source, source.resolveSibling(formattedName));
} catch (IOException e) {
e.printStackTrace();
}
Read more here.
在这里阅读更多。
From the javadocs:
从javadocs:
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.
此方法的行为的许多方面本质上是平台相关的:重命名操作可能无法将文件从一个文件系统移动到另一个文件系统,它可能不是原子的,如果具有目标抽象路径名的文件可能不会成功已经存在。应始终检查返回值以确保重命名操作成功。
Note that the Files class defines the move method to move or rename a file in a platform independent manner.
请注意, Files 类定义了以独立于平台的方式移动或重命名文件的 move 方法。
回答by Awesomedude8888
For the File.renameTo()
to work,The file will need to be somehow writable by external applications.
为了File.renameTo()
工作,该文件需要以某种方式可由外部应用程序写入。
回答by tokhi
You can also do something like below:
您还可以执行以下操作:
File o=new File("oldFile.txt");
File n=new File("newFile.txt");
n.delete();
o.renameTo(n);
n.delete()
: We need to delete the file(new.txt) if exists.
n.delete()
: 我们需要删除文件(new.txt) 如果存在。
o.rename(n)
: so that the file(old.txt) is renamed as new.txt
o.rename(n)
: 这样文件(old.txt) 被重命名为new.txt
How to find out why renameTo() failed?
Reliable File.renameTo() alternative on Windows?
Windows 上的可靠 File.renameTo() 替代方法?
http://www.bigsoft.co.uk/blog/index.php/2010/02/02/file-renameto-always-fails-on-windows
http://www.bigsoft.co.uk/blog/index.php/2010/02/02/file-renameto-always-fails-on-windows
回答by MadProgrammer
We have had issues under Windows 7 with UAC and unexpected file permissions. File#canWrite
will return true even though any attempts to perform file I/O will fail.
我们在 Windows 7 下遇到了 UAC 和意外文件权限问题。 File#canWrite
即使执行文件 I/O 的任何尝试都将失败,也将返回 true。
- Make sure the file you are trying to rename actually exists
- Make sure that the location you are attempting to write the file (or rename the file) to is accessible. We write a simple text file to the location, check to see if it exists and that it's contents is correct (we're paranoid) before we attempt any further I/O.
- 确保您尝试重命名的文件确实存在
- 确保您尝试写入文件(或重命名文件)的位置可访问。我们将一个简单的文本文件写入该位置,在我们尝试任何进一步的 I/O 之前检查它是否存在以及它的内容是否正确(我们很偏执)。
回答by Kaunis
This is working fine for me. Rename is done using two steps but don't forget to set permissions in manifest.xml
with:
这对我来说很好用。重命名使用两个步骤完成,但不要忘记设置权限manifest.xml
:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
public boolean RenameFile(String from, String to) {
to.replace(" ", ""); // clear all spaces within file name
File oldfile = new File(from);
File newfile = new File(to);
File tempfile = new File(to + ".tmp"); // add extension .tmp
oldfile.renameTo(tempfile);
return (tempfile.renameTo(newfile));
}