Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

An example to get last access time of file in java using jdk1.7

Tags:

java

Friends please help. I know that using jdk1.7 we can get the last access time of file. Can anyone give an example with codes to get last access time of file?

like image 948
Rak Avatar asked Jan 31 '12 15:01

Rak


1 Answers

Since you mentioned in your question using jdk1.7 , you should really look into the interface BasicFileAttributes on method lastAccessTime() . I'm not sure what is your real question but if you mean you want an example with codes on reading a file last access time using jdk7, take a look at below.

import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.Files;

/** 
 * compile using jdk1.7
 *
 */
public class ReadFileLastAccess {

    /**
     * @param args
     */
    public static void main(String[] args) throws Exception
    {
        Path file_dir = Paths.get("/home/user/");
        Path file = file_dir.resolve("testfile.txt");
        BasicFileAttributes attrs = Files.readAttributes(file, BasicFileAttributes.class);       
        System.out.println("Last accessed at:" + attrs.lastAccessTime());

    }

}
like image 124
Jasonw Avatar answered Sep 27 '22 15:09

Jasonw