I am looking for a way to read the content of a file in Java without updating the last access time of the file. In C on Linux this is achieved by passing the O_NOATIME flag to open, and I'm sure other languages and platforms have similar mechanisms.
I'm using Files.newInputStream which has various options that can be passed, but none that appear to match O_NOATIME.
Is there a way to read the content of a file in Java on a unix-like platform without updating the last access time of the file?
I don't think there's a way to do this in Java (apart from implementing it natively with JNI OFC). I grepped JDK sources at Github for instances of
O_NOATIMEand didn't get any hits.If you peek into the UnixChannelFactory class you can see the Flags class which, I think, lists all supported options:
As a workaround you can revert the last access time after accessing the file:
The
getLastAccessTimefunction is: