Note that there are some explanatory texts on larger screens.

plurals
  1. POfile.createNewFile() creates files with last-modified time before actual creation time
    primarykey
    data
    text
    <p>I'm using <a href="http://jpoller.sourceforge.net/" rel="nofollow noreferrer">JPoller</a> to detect changes to files in a specific directory, but it's missing files because they end up with a timestamp earlier than their actual creation time. Here's how I test:</p> <pre><code>public static void main(String [] files) { for (String file : files) { File f = new File(file); if (f.exists()) { System.err.println(file + " exists"); continue; } try { // find out the current time, I would hope to assume that the last-modified // time on the file will definitely be later than this System.out.println("-----------------------------------------"); long time = System.currentTimeMillis(); // create the file System.out.println("Creating " + file + " at " + time); f.createNewFile(); // let's see what the timestamp actually is (I've only seen it &lt;time) System.out.println(file + " was last modified at: " + f.lastModified()); // well, ok, what if I explicitly set it to time? f.setLastModified(time); System.out.println("Updated modified time on " + file + " to " + time + " with actual " + f.lastModified()); } catch (IOException e) { System.err.println("Unable to create file"); } } } </code></pre> <p>And here's what I get for output:</p> <pre><code>----------------------------------------- Creating test.7 at 1272324597956 test.7 was last modified at: 1272324597000 Updated modified time on test.7 to 1272324597956 with actual 1272324597000 ----------------------------------------- Creating test.8 at 1272324597957 test.8 was last modified at: 1272324597000 Updated modified time on test.8 to 1272324597957 with actual 1272324597000 ----------------------------------------- Creating test.9 at 1272324597957 test.9 was last modified at: 1272324597000 Updated modified time on test.9 to 1272324597957 with actual 1272324597000 </code></pre> <p>The result is a race condition:</p> <ol> <li>JPoller records time of last check as xyz...123</li> <li>File created at xyz...456</li> <li>File last-modified timestamp actually reads xyz...000</li> <li>JPoller looks for new/updated files with timestamp greater than xyz...123</li> <li>JPoller ignores newly added file because xyz...000 is less than xyz...123</li> <li>I pull my hair out for a while</li> </ol> <p>I tried digging into the code but both <a href="http://java.sun.com/javase/6/docs/api/java/io/File.html#lastModified()" rel="nofollow noreferrer"><code>lastModified()</code></a> and <a href="http://java.sun.com/javase/6/docs/api/java/io/File.html#createNewFile()" rel="nofollow noreferrer"><code>createNewFile()</code></a> eventually resolve to native calls so I'm left with little information.</p> <p>For <code>test.9</code>, <b>I lose 957 milliseconds</b>. What kind of accuracy can I expect? Are my results going to vary by operating system or file system? Suggested workarounds?</p> <p>NOTE: I'm currently running Linux with an XFS filesystem. I wrote a quick program in <code>C</code> and the <a href="http://linux.die.net/man/2/stat" rel="nofollow noreferrer">stat system call</a> shows <code>st_mtime</code> as <code>truncate(xyz...000/1000)</code>.</p> <p><strong>UPDATE</strong>: I ran the same program I have above on Windows 7 with NTFS and it <strong>does</strong> maintain full millisecond accuracy. The <a href="http://msdn.microsoft.com/en-us/library/ms724290%28VS.85%29.aspx" rel="nofollow noreferrer">MSDN link</a> @mdma provided further notes that FAT filesystems is accurate for creates with 10 ms resolution but for access is only accurate to 2 seconds. Thus, this is truly OS dependent.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload