Preserve file creation time with Java

前端 未结 2 1279
别跟我提以往
别跟我提以往 2021-01-24 14:36

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

2条回答
  •  甜味超标
    2021-01-24 14:51

    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);
    

提交回复
热议问题