Iterating a DirectoryStream and changing contents of a directory at the same time

directory-structure, file-io, java, java-7

Solution

The documentation is intentionally vague. The JVM has to run on a number of different kinds of machines: Windows and Unix-derivatives. Different file systems have different behaviors. You must (I repeat, MUST) design for the worst case if you want your program to work reliably on more than one computer.

The law of least surprise suggests that you should slurp up the entire DirectoryStream to get a snapshot (or very close to one), iterate over the snapshot, and then re-slurp the stream. You can then compare the different versions of the snapshots to determine changes to the underlying directory.

Problem

The documentation of DirectoryStream clearly states: The iterator is weakly consistent. It is thread safe but does not freeze the directory while iterating, so it may (or may not) reflect updates to the directory that occur after the DirectoryStream is created. On my machine, I executed a simple iteration over a directory in debug mode. Before the iteration completed, I broke execution, added a file to the directory being iterated and resumed. The iteration did not see the extra file. My question: under what circumstance will the iteration reflect updates to the directory contents? Unfortunately the formal documentation is very vague about it. To say the least.

Original source