user3143553
user3143553

Reputation:

Preserve file creation time with Java

I wrote a little copy tool in java to copy .mp3 files on my USB stick. When new files are copied, some file attributes are preserved, but not the creation time.

To copy files I use:

org.apache.commons.io.FileUtils
static void copyFile(File srcFile, File destFile, boolean preserveFileDate)

FileUtils.copyFile(sourceFile, newTargetFile, preserveFileDate);

So my question, is there a way to preserve the file creation time? If not, I think I set up a class to copy with windows robocopy.

Upvotes: 2

Views: 2315

Answers (2)

assylias
assylias

Reputation: 328618

If you are using Java 7+, you can use:

Files.copy(source, target, StandardCopyOption.COPY_ATTRIBUTES);

If that does not copy the creation time (it does on my machine), you can also manually set it:

Path source = ...;
Path target = ...;
Files.copy(source, target, StandardCopyOption.COPY_ATTRIBUTES);

FileTime creationTime  = (FileTime) Files.readAttributes(source, "creationTime").get("creationTime");
Files.setAttribute(target, "creationTime", creationTime);

Upvotes: 7

dawww
dawww

Reputation: 371

Take a look at: http://docs.oracle.com/javase/tutorial/essential/io/fileAttr.html#basic

It's possible to get and set files attributes like creation time.

Upvotes: 0

Related Questions