Preserve file creation time with Java

前端 未结 2 1248
别跟我提以往
别跟我提以往 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);
    
    0 讨论(0)
  • 2021-01-24 15:15

    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.

    0 讨论(0)
提交回复
热议问题