Moving files with java.nio it will figure out if you are moving a file on the same partition or to a different partition.
By looking through the code below you can see.
http://hg.openjdk.java.net/jdk8/jdk8/jdk/file/687fd7c7986d/src/share/classes/java/nio/file/Files.java
This is the move method in the Files class. It decides if should do a move or
copy based on if the file is moving with in the same file system or to another one.
FileSystemProvider is an abstract class that provides methods for concrete classes to provide a move method that is associated with the file system.
- If you are moving on the same file system then it will do a move which is faster than a copy.
- If you moving to another filesystem then it will do a copy.
By looking through the code below you can see.
http://hg.openjdk.java.net/jdk8/jdk8/jdk/file/687fd7c7986d/src/share/classes/java/nio/file/Files.java
This is the move method in the Files class. It decides if should do a move or
copy based on if the file is moving with in the same file system or to another one.
public static Path move(Path source, Path target, CopyOption... options)
throws IOException
{
FileSystemProvider provider = provider(source);
if (provider(target) == provider) {
// same provider
provider.move(source, target, options);
} else {
// different providers
CopyMoveHelper.moveToForeignTarget(source, target, options);
}
return target;
}
FileSystemProvider is an abstract class that provides methods for concrete classes to provide a move method that is associated with the file system.
You will have a different move implementations for every file system.
For linux filesystems this class provides the move method that just calls UnixCopyFile.move()
In this class it does a rename on the files from source to target. If it can not complete that job then it will just do a copy which will transfer the bytes. Look at the code below. The exception can be ignored.
If the rename exception is not bad enough it will ignore and continue with a copy. // first try rename
try {
rename(source, target); ######
return;
} catch (UnixException x) {
if (x.errno() != EXDEV && x.errno() != EISDIR) {
x.rethrowAsIOException(source, target);
}
}
// copy source to target
if (sourceAttrs.isDirectory()) {
copyDirectory(source, sourceAttrs, target, flags);
} else {
if (sourceAttrs.isSymbolicLink()) {
copyLink(source, sourceAttrs, target, flags);
} else {
if (sourceAttrs.isDevice()) {
copySpecial(source, sourceAttrs, target, flags);
} else {
copyFile(source, sourceAttrs, target, flags, 0L); ######
}
}
}
This C file has the native JNI rename method for linux
In the end you could be faster to do a rename instead of a move if you know your application is working on the same file system. Less decisions will be made by java for you.
No comments:
Post a Comment