How use Java FileChannel to copy preserving timestamps for files and directories? Looks like the files are not preserving timestamps while copying to another location. How is that possible using FileChannel in Java?
Asked
Active
Viewed 2,310 times
2 Answers
2
This is not the role of FileChannel
. A FileChannel
is just a wrapper over a byte channel.
What you want instead is to use the "new" Java 7 file API for that. If you want to copy a file to a location while preserving attributes you can do it:
Files.copy(src, dst, StandardCopyOption.COPY_ATTRIBUTES);

fge
- 119,121
- 33
- 254
- 329
-
It should be noted that, as specified by its javadoc, `COPY_ATTRIBUTES` is only 'best effort basis'. Except 'modified timestamp', nothing else is guaranteed. – Manu Manjunath Mar 15 '20 at 15:24
1
You can not do it by FileChannel
, you can use apache commons io :
IOUtils.copy(new FileInputStream(file), new FileOutputStream(file2));
// copy file and preserve the time stamp. the sourceFile and destFile are of type java.io.File
FileUtils.copyFile(sourceFile,destFile);
Reference : http://www.studytrails.com/java-io/file-copying-and-moving-deleting.jsp

Shreyos Adikari
- 12,348
- 19
- 73
- 82