Here's the basic setup, writing to a text file.
Java 11.0.13 Corretto, Windows 10.
Files.write(path, stuff);
System.out.println(((FileTime) Files.getAttribute(p, "lastAccessTime")).toMillis());
System.out.println(((FileTime) Files.getAttribute(p, "lastModifiedTime")).toMillis());
Thread.sleep(100);
System.out.println(((FileTime) Files.getAttribute(p, "lastAccessTime")).toMillis());
System.out.println(((FileTime) Files.getAttribute(p, "lastModifiedTime")).toMillis());
I'd expect all 4 times to be the same. But what actually happens is something like this:
1642405963200
1642405963200
1642405963207
1642405963200
For some reason, the last access time is updated to a few milliseconds later (7 in this particular run).
What's the reason for this behavior?
EDIT: Further testing shows this only occurred to to the usage of the iterable parameter of the write method.
The following results in the strange behavior shown above.
Files.write(path, Arrays.asList("1", "2", "3"));
Either of the following does not, and last access time is consistent as expected.
Files.write(p, "1\n2\n3\n".getBytes());
Files.writeString(p, "1\n2\n3\n");
`