Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java fails in moving (renaming) a file when the resulting file is on another filesystem

A program we have erred when trying to move files from one directory to another. After much debugging I located the error by writing a small utility program that just moves a file from one directory to another (code below). It turns out that while moving files around on the local filesystem works fine, trying to move a file to another filesystem fails.

Why is this? The question might be platform specific - we are running Linux on ext3, if that matters.

And the second question; should I have been using something else than the renameTo() method of the File class? It seems as if this just works on local filesystems.

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

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"));
    }
}
like image 945
oligofren Avatar asked May 10 '12 09:05

oligofren


People also ask

How do I move a file from one location to another in Java?

You can move a file or directory by using the move(Path, Path, CopyOption...) method. The move fails if the target file exists, unless the REPLACE_EXISTING option is specified.

How do you rename and move a file in Java?

To rename or move a file/directory in Java, you can use either the renameTo() method of a File object in the old File I/O API, or the Files. move() method in the new Java NIO API.

Can we rename a file in Java?

Rename operation is possible using renameTo() method belongs to the File class in java. The renameTo() method is used to rename the abstract pathname of a file to a given pathname. The method returns a boolean value i.e. returns true if the file is renamed else returns false.

Which method is used to rename the file in Java?

In Java we can rename a file using renameTo(newName) method that belongs to the File class. Parameters: dest – The new abstract pathname for the existing abstract pathname.


2 Answers

Java 7 and above

Use Files.move(Path source, Path target, CopyOption... opts).

Note that you must not provide the ATOMIC_MOVE option when moving files between file systems.

Java 6 and below

From the docs of File.renameTo:

[...] The rename operation might not be able to move a file from one filesystem to another [...]

The obvious workaround would be to copy the file "manually" by opening a new file, write the content to the file, and delete the old file.

You could also try the FileUtils.moveFile method from Apache Commons.

like image 176
aioobe Avatar answered Oct 12 '22 13:10

aioobe


Javadoc to the rescue:

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.

like image 34
JB Nizet Avatar answered Oct 12 '22 13:10

JB Nizet