8

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

yuris
  • 1,109
  • 4
  • 19
  • 33

5 Answers5

16

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.

Community
  • 1
  • 1
Jeroen Vannevel
  • 43,651
  • 22
  • 107
  • 170
  • Worked like a charm on my Windows 7 - especially when adding the REPLACE_EXISTING option, and optionally the ATOMIC_MOVE option. Thank you. – Terje Dahl Jan 04 '18 at 10:34
0

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

0

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

Community
  • 1
  • 1
tokhi
  • 21,044
  • 23
  • 95
  • 105
  • I'll try to use the delete first, but what if the delete fails (if the file is somehow locked i won't be able to delete it)? – yuris Dec 11 '12 at 19:30
0

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.
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • btw ,canwrite returns true – yuris Dec 11 '12 at 19:47
  • *"File#canWrite will return `true` even though any attempts to perform file I/O will fail"* - We expereinced a situation under Windows 7 where the `File#canWrite` could not detect the UAC restrictions, so file the method returned `true`, any attempt to perform file I/O in the specified location would fail (usually silently) – MadProgrammer Dec 11 '12 at 19:59
  • The only work arounds we've come up with is 1- Write a temp file and check that it's exists and it's contents are correct or 2- Copy the file manually. Java 7 (reportedly) fixes the issues with the UAC, but we've not had the chance to test it. – MadProgrammer Dec 11 '12 at 20:23
0

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));
}
Arman H
  • 5,488
  • 10
  • 51
  • 76
Kaunis
  • 11
  • 3