Probably a simple answer but I've tried reading the javadoc documentation on StandardOpenOption but I'm still unclear of what happens when I say
Files.write(..., ..., StandardOpenOption.WRITE, StandardOpenOption.CREATE); // Write A
Based on some local tests, it looks like this will still overwrite the file if it exists already, so what is the difference between the above and
Files.write(..., ..., StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.CREATE); // Write B
?
Furthermore, I have a program with two threads that read/write from the written file. When I use Write B, I sometimes get a race condition where one thread would've created the file, and the other overwrites it while the first thread is reading and I get an exception. But when I use Write A, I never get this race condition. Almost as if it'll lock the file first? Can someone explain what is going on behind the scenes?