2

I am using javxt.io.file library to retrive lastAccesstime of specific file but i am getting the same result even though it is accessed several times .please let mw know if i am doing any mistake?

public class LastAccessTime 
{
     public static void main(String[] args) {
            javaxt.io.File file = new javaxt.io.File("C:/Users/IISU24/Desktop/calculator/rahul.txt");
            System.out.println("Accessed: " + file.getLastAccessTime());
     }
}
Yubaraj
  • 3,800
  • 7
  • 39
  • 57
  • Are you able to use Java 7? – MadProgrammer Apr 16 '14 at 05:47
  • yes i am using java 7 C:\Users\IISU24>java -version java version "1.7.0_07" Java(TM) SE Runtime Environment (build 1.7.0_07-b11) Java HotSpot(TM) Client VM (build 23.3-b01, mixed mode, sharing) – RahulRock27 Apr 16 '14 at 05:49
  • possible duplicate: http://stackoverflow.com/questions/920259/getting-the-last-access-time-for-a-file-in-java – jyotesh Apr 16 '14 at 05:52
  • From the JavaDoc for BasicFileAttributes.lastAccessTime(): If the file system implementation does not support a time stamp to indicate the time of last access then this method returns an implementation specific default value, typically the last-modified-time or a FileTime representing the epoch (1970-01-01T00:00:00Z) – RahulRock27 Apr 16 '14 at 06:19

3 Answers3

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

public class ReadFileLastAccess {
    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());
    }
}
Taylor Hx
  • 2,815
  • 23
  • 36
Sunny
  • 308
  • 2
  • 14
  • From the JavaDoc for [BasicFileAttributes.lastAccessTime()](http://docs.oracle.com/javame/config/cldc/opt-pkgs/api/cldc/api/java/nio/file/attribute/BasicFileAttributes.html#lastAccessTime()): _If the file system implementation does not support a time stamp to indicate the time of last access then this method returns an implementation specific default value, typically the last-modified-time or a FileTime representing the epoch (1970-01-01T00:00:00Z)._ – Taylor Hx Apr 16 '14 at 06:01
1

you cannot do it with File, try Java 7 NIO2

FileTime attr = (FileTime) Files.getAttribute(Paths.get("1.txt"), "lastAccessTime");
Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
  • 1
    From the JavaDoc for BasicFileAttributes.lastAccessTime(): If the file system implementation does not support a time stamp to indicate the time of last access then this method returns an implementation specific default value, typically the last-modified-time or a FileTime representing the epoch (1970-01-01T00:00:00Z). – RahulRock27 Apr 16 '14 at 06:26
0

The NIO package is your friend :)

import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;

public class Snippet {

    public static void main(String[] args) throws IOException {
        Path file = Paths.get("target.target");
        BasicFileAttributes attrs = Files.readAttributes(file, BasicFileAttributes.class);
        System.out.println("Last access time:" + attrs.lastAccessTime());
    }
}
Stefan S.
  • 3,950
  • 5
  • 25
  • 77
  • From the JavaDoc for [BasicFileAttributes.lastAccessTime()](http://docs.oracle.com/javame/config/cldc/opt-pkgs/api/cldc/api/java/nio/file/attribute/BasicFileAttributes.html#lastAccessTime()): _If the file system implementation does not support a time stamp to indicate the time of last access then this method returns an implementation specific default value, typically the last-modified-time or a FileTime representing the epoch (1970-01-01T00:00:00Z)._ – Taylor Hx Apr 16 '14 at 06:01
  • FileTime lastAccessTime() Returns the time of last access. If the file system implementation does not support a time stamp to indicate the time of last access then this method returns an implementation specific default value, typically the last-modified-time or a FileTime representing the epoch (1970-01-01T00:00:00Z). – RahulRock27 Apr 16 '14 at 06:17