java - file.createNewFile() creates files with last-modified time before actual creation time -


i'm using jpoller detect changes files in specific directory, it's missing files because end timestamp earlier actual creation time. here's how test:

public static void main(string [] files) {     (string file : files)     {         file f = new file(file);         if (f.exists())         {             system.err.println(file + " exists");             continue;         }          try         {             // find out current time, hope assume last-modified             // time on file later             system.out.println("-----------------------------------------");             long time = system.currenttimemillis();              // create file             system.out.println("creating " + file + " @ " + time);             f.createnewfile();              // let's see timestamp (i've seen <time)             system.out.println(file + " last modified at: " + f.lastmodified());              // well, ok, if explicitly set time?             f.setlastmodified(time);             system.out.println("updated modified time on " + file + " " + time + " actual " + f.lastmodified());         }         catch (ioexception e)         {             system.err.println("unable create file");         }     } } 

and here's output:

----------------------------------------- creating test.7 @ 1272324597956 test.7 last modified at: 1272324597000 updated modified time on test.7 1272324597956 actual 1272324597000 ----------------------------------------- creating test.8 @ 1272324597957 test.8 last modified at: 1272324597000 updated modified time on test.8 1272324597957 actual 1272324597000 ----------------------------------------- creating test.9 @ 1272324597957 test.9 last modified at: 1272324597000 updated modified time on test.9 1272324597957 actual 1272324597000 

the result race condition:

  1. jpoller records time of last check xyz...123
  2. file created @ xyz...456
  3. file last-modified timestamp reads xyz...000
  4. jpoller looks new/updated files timestamp greater xyz...123
  5. jpoller ignores newly added file because xyz...000 less xyz...123
  6. i pull hair out while

i tried digging code both lastmodified() , createnewfile() resolve native calls i'm left little information.

for test.9, i lose 957 milliseconds. kind of accuracy can expect? results going vary operating system or file system? suggested workarounds?

note: i'm running linux xfs filesystem. wrote quick program in c , stat system call shows st_mtime truncate(xyz...000/1000).

update: ran same program have above on windows 7 ntfs , does maintain full millisecond accuracy. msdn link @mdma provided further notes fat filesystems accurate creates 10 ms resolution access accurate 2 seconds. thus, os dependent.

filesystems not store time precisely, , not @ millisecond resolution, e.g. fat has 2-second resolution creation time, , ntfs can delay updating last access time hour. (details on msdn.) although not in case, in general, there problem of synchronizing clocks if file created on computer.

it seems might issue jpoller folks, since time handling logic is. until it's fixed, workaround manually setting last modified time of each file written +4 seconds actual time - +4 arbitrary value should larger resolution of filesystem working on. when files written file system, rounded down, less value have added. not pretty, work!


Comments

Popular posts from this blog

javascript - Enclosure Memory Copies -

php - Replacing tags in braces, even nested tags, with regex -