Reputation:
is there a way in java to access the change time attribute of a file on linux?
I tried to use java.io.File.lastModified() method, but this method is returning only modify time attribute for the file.
What I want to do is to detect the time of upload of a file to a linux server. I noticed that modify time attribute has the timestamp of file modification on my local machine, however, the change time attribute is the time of upload to a linux server.
Thank you for any advice.
Upvotes: 2
Views: 1562
Reputation: 31
You can use the following method to get change time of a file.
Files.getAttribute(new File("<path to a file>").toPath(),"unix:ctime")
Here is the example code:
import java.util.*;
import java.io.*;
import java.nio.file.*;
import java.nio.file.attribute.*;
public class Test{
public static void main(String[] args){
try
{
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:
{ctime=2018-06-26T02:17:06.009384Z}
As you may see it changed the change time hour from `10` to `02`.
Upvotes: 3