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?
Right click on the files/folders select Properties. Select the Security tab. Click the Advanced button. Select the Audit tab.
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.
ls -ltu list all the files, showing and sorting by access time.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With