1

Possible Duplicate:
Getting the last access time for a file in Java

I'm using JDK 1.6 and tried various times for getting the Date and time of last access of a file.

But could not find exact solution.

Community
  • 1
  • 1
user1693843
  • 11
  • 1
  • 3
  • This link shows you how to get *last access* of a file and not just last modified: [Getting the last access time for a file in Java](http://stackoverflow.com/questions/920259/getting-the-last-access-time-for-a-file-in-java) – maba Sep 25 '12 at 12:45
  • The last access time isn't very reliable. If I do a word search or backup a directory, it could result in every file being accessed. Some filesystems don't even support access date/times – Peter Lawrey Sep 25 '12 at 12:48
  • Last access what? Last read, modify, create or what? – Roman C Sep 25 '12 at 12:53
  • last access means last time opened,read? – user1693843 Sep 25 '12 at 12:55
  • sir,Plz give me the soultion so that i can get the last access,creation date of a file in java6 only and its very urgent for me? – user1693843 Sep 26 '12 at 05:04

3 Answers3

1

File has lastModified() method, which you can use.

Azodious
  • 13,752
  • 1
  • 36
  • 71
0

java.io.File#lastModified() returns the date when the file is last modified as a long, i.e number of milliseconds since jan 1 1970. You can set that in a java.util.Date or java.util.Calendar to get human readable date.

Vikdor
  • 23,934
  • 10
  • 61
  • 84
0

In 1.6 as people have said you can get the last modified date. Not hte last accessed date though.

If you can access to java 7 then there is the BasicFileAttibutes interface that would give you this information. For example

Path file = ...
BasicFileAttributes attrs = Files.readAttributes(file, BasicFileAttributes.class);
attrs.lastAccessTime();
RNJ
  • 15,272
  • 18
  • 86
  • 131
  • 1
    but it is the requirement of head that i have do it only in java 6 – user1693843 Sep 25 '12 at 12:50
  • then I think you can only get the lastModifiedTime unless you use JNI and talk to the OS directly. Last accessed is not too reliable. Searching a directory can give false results on this – RNJ Sep 25 '12 at 12:52
  • what is JNI and how can we use JNI. Can u please elaborate? – user1693843 Sep 25 '12 at 12:56
  • 1
    JNI = [Java Native Interface](http://docs.oracle.com/javase/6/docs/technotes/guides/jni/) - a way to call native code from Java (and vice versa). – Jesper Sep 25 '12 at 13:15
  • 1
    Last Access Time Knowing when a file or directory was last accessed is a key attribute for monitoring and auditing file systems. This important attribute is available on both Windows and Unix/Linux file systems. The javaxt.io.File and javaxt.io.Directory classes both provide methods to retrieve the last access time. Here's an example of how to retrieve the last access time for a file: javaxt.io.File file = new javaxt.io.File("/temp/file.txt"); System.out.println("Accessed: " + file.getLastAccessTime()); – user1693843 Oct 01 '12 at 11:19
  • 1
    javaxt is an extension to the core library. My solution uses java 7 core libraries – RNJ Oct 01 '12 at 11:23