Java cannot move (rename) a file when the resulting file is on a different file system

The program that we allowed when trying to move files from one directory to another. After a lot of debugging, I found an error by writing a small utility program that simply moves the file from one directory to another (code below). It turns out that when moving files around the local file system it works fine, an attempt to transfer the file to another file system fails.

Why is this? The question may be platform specific - we use Linux on ext3, if that matters.

And the second question; should I use something else than the renameTo() method of the File class? This seems to just work on local file systems.

Tests (run as root):

 touch /tmp/test/afile java FileMover /tmp/test/afile /root/ The file move was successful touch /tmp/test/afile java FileMover /tmp/test/afile /some_other_disk/ The file move was erroneous 

the code:

 import java.io.File; public class FileMover { public static void main(String arguments[] ) throws Exception { boolean success; File file = new File(arguments[0]); File destinationDir = new File(arguments[1]); File destinationFile = new File(destinationDir,file.getName() ); success = file.renameTo(destinationFile); System.out.println("The file move was " + (success?"successful":"erroneous")); } } 
+7
source share
4 answers

From the File.renameTo :

[...] The renaming operation may not transfer a file from one file system to another [...]

An obvious workaround would be to copy the file β€œmanually” by opening a new file, write the contents to the file, and delete the old file.

You can also try FileUtils.moveFile from Apache Commons.

+16
source

Javadoc to the rescue:

Many aspects of the behavior of this method are inherently platform-dependent: the renaming operation may not be a file from one file system to another, it may not be atomic, or it may not work if a file with the target destination name already exists. You always need to check the return value to make sure that the rename operation is successful.

Note that the Files class defines a move method for moving or renaming a file in an independent platform.

+6
source

From the docs:

Renames the file indicated by this abstract path.

Many aspects of the behavior of this method are inherently platform-dependent: the renaming operation may not be a file from one file system to another, it may not be atomic, or it may not work if a file with the target destination name already exists. You always need to check the return value to make sure that the rename operation is successful.

If you want to move a file between different file systems, you can use Apache moveFile

+1
source

your identifier is a beause / some_other_disk / relative url error, but completely url, cannot find the url I have an example

java FileMover D: \ Eclipse33_workspace_j2ee \ test \ src \ a \ a.txt D: \ Eclipse33_workspace_j2ee \ test \ src The file was successful

java FileMover D: \ Eclipse33_workspace_j2ee \ test \ src \ a \ a.txt \ Eclipse33_workspace_j2ee \ test \ src

The file movement was dissimilar

the result is the error url

-one
source

All Articles