File.renameTo() fails

2020-02-01 20:23发布

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.

But sometimes file is not renamed!

I tried to add some Thread.Sleep(BIG_NUMBER) between couple of retries this didn't help.

It looks like the file getting some kind of lock. (when I kill the jvm I can rename the file).

Is there something I can do?

OS: Windows XP, windows 7 JAVA version: 1.5

标签: java file
5条回答
我只想做你的唯一
2楼-- · 2020-02-01 20:48

For the File.renameTo() to work,The file will need to be somehow writable by external applications.

查看更多
爷、活的狠高调
3楼-- · 2020-02-01 20:57

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.

o.rename(n) : so that the file(old.txt) is renamed as new.txt

How to find out why renameTo() failed?

Reliable File.renameTo() alternative on Windows?

http://www.bigsoft.co.uk/blog/index.php/2010/02/02/file-renameto-always-fails-on-windows

查看更多
霸刀☆藐视天下
4楼-- · 2020-02-01 20:58

This is working fine for me. Rename is done using two steps but don't forget to set permissions in manifest.xml with:

<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));
}
查看更多
小情绪 Triste *
5楼-- · 2020-02-01 21:00

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

Path source = currentFile.toPath();
try {
     Files.move(source, source.resolveSibling(formattedName));
} catch (IOException e) {
     e.printStackTrace();
}

Read more here.

From the 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.

查看更多
倾城 Initia
6楼-- · 2020-02-01 21:08

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.

  1. Make sure the file you are trying to rename actually exists
  2. 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.
查看更多
登录 后发表回答