2

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.

  • There is no "java" way of doing this, as described by [this comment](http://stackoverflow.com/a/16127460/7470253) on similar question: there is no simple way of accessing a file change time. You might be able to fish something from linux implementation of JVM (doubt it) or write a shell script to read this attribute. Or change uploader in such a way that it force-sets the LastModified time on your file every time. – M. Prokhorov Apr 04 '17 at 15:15
  • Why do care that it's specifically a change time though? Last Modified time captures last time when file contents were changed. If I didn't change any contents, why do you have to react? – M. Prokhorov Apr 04 '17 at 15:16
  • I have a program that stores the time from a file to the database (currently modify time). There could be more people with different versions of that file and they upload that file to the linux server. I want to store the time of last upload (change time) and not the time of last modification (just in case someone will upload the file after couple hours after modification). In this case is necessary to upload the file right after the modification. –  Apr 05 '17 at 09:48

1 Answers1

3

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`.
Justin Lee
  • 31
  • 3