Reputation: 16129
Is there a way in java to set the access time without setting the date modified time as well?
This will be in essence a touch -a command.
the setLastModified method in File updates both the access time as well as the date modified.
We are currently using java 6. Moving to 7 wouldn't be out of the question.
Upvotes: 4
Views: 3340
Reputation: 7102
There is a private sun API to check access time on a file(How to prevent ShellFolder.getFolderColumns(...) from crashing a lot), but no way to set it from what I've found. Though access time should be updated everytime you open the file, so maybe try opening it in Java.
You could check if the old JDIC project has the option: http://javadesktop.org/articles/jdic/index.html
Or this library: http://www.teamdev.com/jxfilewatcher/
Otherwise you might have to look up the console command for the OS's you want to support and call from Java.
Upvotes: 0
Reputation: 691685
I guess opening an input stream on the file should modify its access time. But I don't know of any API to directly modify this attribute in Java 6.
Upvotes: 1
Reputation: 308021
You can use Files.setAttribute()
from Java 7:
FileTime fileTime = FileTime.fromMillis(millis);
Files.setAttribute(path, "lastAccessTime", fileTime);
The string "lastAccessTime"
can be found in the description of the BasicFileAttributeView
, which also provides an alternative way to set this property (together with Files.getFileAttributeView()
):
Files.getFileAttributeView(path, BasicFileAttributeView.class).setTimes(null, fileTime, null);
I'm not aware of any pure Java way that works in Java 6 or earlier.
Upvotes: 7