你可以使用下面的方法来获得一个文件的变化时间。
Files.getAttribute(new File("<path to a file>").toPath(),"unix:ctime")
以下是示例代码。
import java.util.*;
import java.io.*;
import java.nio.file.*;
import java.nio.file.attribute.*;
public class Test{
public static void main(String[] args){
System.out.println(Files.getAttribute(new File("b").toPath(),"unix:ctime"));
System.out.println(new Date(((FileTime)Files.getAttribute(new File("b").toPath(),"unix:ctime")).toMillis()));
catch (Exception e)
e.printStackTrace();
When running `LANG=en_us.UTF-8 stat b` on the console, the output is:
File: 'b'
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: fd00h/64768d Inode: 101210225 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Context: unconfined_u:object_r:admin_home_t:s0
Access: 2018-06-26 10:16:18.649378326 +0800
Modify: 2018-06-26 10:16:18.649378326 +0800
Change: 2018-06-26 10:17:06.009384678 +0800
Birth: -
Running the example code with the `Test` class provides the following output: