File.renameTo() fails

后端 未结 5 2159
半阙折子戏
半阙折子戏 2020-12-06 18:24

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

相关标签:
5条回答
  • 2020-12-06 18:43

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

    0 讨论(0)
  • 2020-12-06 18:46

    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.
    0 讨论(0)
  • 2020-12-06 18:52

    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));
    }
    
    0 讨论(0)
  • 2020-12-06 18:57

    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.

    0 讨论(0)
  • 2020-12-06 18:58

    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

    0 讨论(0)
提交回复
热议问题