What are the differences between them and how do I choose which one to use ?
-
1Same as move versus copy in any file system. Nothing Java-specific about it. – user207421 Aug 06 '13 at 05:11
-
You need to know if you want to copy a file (keep the original), or rename it (change the location of the original). – Peter Lawrey Aug 06 '13 at 08:21
2 Answers
File.renameTo() changes the name of a file. If the target filename is on another filesystem, it may copy the file's contents, but this is platform dependent.
NIO's FileChannel.transferTo() method actually copies the contents (i.e. the bytes) of a FileChannel (e.g. a file) to another location.
So if you are using renameTo() on the same filesystem, no bytes will be read/writen, only the filename in the directory listing will be changed.

- 1,551
- 1
- 13
- 17
Java NIO
1- In Java NIO you can transfer data directly from one channel to another.
2- Data can be transferred using the transferTo(..) and transferFrom(..) methods of the java.nio.channels.FileChannel class.
3- These methods use the underlying optimization of the file system and therefore in certain cases data transfer can be fast, especially for large files. However, note that the implementation is file system specific and it would be false to claim that this method is always faster
File .Rename to
Java.io.File does not contains any ready make move file method, but you can workaround with the following two alternatives : 1 -File.renameTo(). 2-Copy to new file and delete the original file
renameTo does not work if your target path is on a different filesystem. It will simply return false

- 2,324
- 1
- 15
- 17