Setting/changing the ctime or “Change time” attribute on a file

前端 未结 2 1865
刺人心
刺人心 2020-12-10 07:53

I wish to change the timestamp metadata on files in Java using the java.nio.Files class.

I would like to change all 3 Linux/ext4 timestamps (last modifi

2条回答
  •  一个人的身影
    2020-12-10 08:10

    Adapting this answer to your case:

    // Warning: Disk must be unmounted before this operation
    String disk = "/dev/sda1";
    // Update ctime
    Runtime.getRuntime().exec("debugfs -w -R 'set_inode_field "+pathToMyFile+" ctime "+myCustomTime+"' "+disk);
    // Drop vm cache so ctime update is reflected
    Runtime.getRuntime().exec("echo 2 > /proc/sys/vm/drop_caches");
    

    I doubt we will see a convenient method in Standard Java API to do this, as neither Linux (man touch) nor Windows (GetFileTime function on MSDN) give easy access to this field. Native system calls give only access to creation/access/modify timestamps, so does Java.

提交回复
热议问题