Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the Last Access Time for a File

I know that using File object we can get the last modified time for a File (i.e. File.lastModified()). But, my requirement is to get the last accessed time for a File in Java. How do I get it?

like image 488
Veera Avatar asked May 28 '09 10:05

Veera


People also ask

Can you see the last time a file was accessed?

Right click on the files/folders select Properties. Select the Security tab. Click the Advanced button. Select the Audit tab.

What is last access time of a file?

Last access timestamp of a file is the last date and time when that file was opened for reading or writing. So every time a user access a file this timestamp needs to be updated, which is a bit of an overhead especially if you are not too keen on this file attribute.

Which command will show the last access time of a file in Unix?

ls -ltu list all the files, showing and sorting by access time.


1 Answers

You will need to use the new file I/O API (NIO2) which comes with Java 7. It has a method lastAccessTime() for reading the last access time.

Here is a usage example:

Path file = ...
BasicFileAttributes attrs = Files.readAttributes(file, BasicFileAttributes.class);
FileTime time = attrs.lastAccessTime();

For more information see Managing Metadata in the Java Tutorial.

like image 189
Esko Luontola Avatar answered Oct 02 '22 22:10

Esko Luontola