title: How to guarantee atomic move or exception of a file in Java?
body: <p>In one of my projects I have concurrent write access to one single file within one JRE and want to handle that by first writing to a temporary file and afterwards moving that temp file to the target using an atomic move. I don't care about the order of the write access or such, all I need to guarantee is that any given time the single file is usable. I'm already aware of Files.move and such, my problem is that I had a look at at least one implementation for that method and it raised some doubts about if implementations really guarantee atomic moves. Please look at the following code:</p>
  
  <p><a href="http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/7u40-b43/java/nio/file/Files.java#Files.move%28java.nio.file.Path%2Cjava.nio.file.Path%2Cjava.nio.file.CopyOption%5B%5D%29" rel="noreferrer">Files.move on GrepCode for OpenJDK</a></p>
  
  <pre><code>1342        FileSystemProvider provider = provider(source);
  1343        if (provider(target) == provider) {
  1344            // same provider
  1345            provider.move(source, target, options);
  1346        } else {
  1347            // different providers
  1348            CopyMoveHelper.moveToForeignTarget(source, target, options);
  1349        }
  </code></pre>
  
  <p>The problem is that the option ATOMIC_MOVE is not considered in all cases, but the location of the source and target path is the only thing that matters in the first place. That's not what I want and how I understand the documentation:</p>
  
  <blockquote>
    <p>If the move cannot be performed as an atomic file system operation then
    AtomicMoveNotSupportedException is thrown. This can arise, for example, when the target
    location is on a different FileStore and would require that the file be copied, or target
    location is associated with a different provider to this object.</p>
  </blockquote>
  
  <p>The above code clearly violates that documentation because it falls back to a copy-delete-strategy without recognizing ATOMIC_MOVE at all. An exception would be perfectly OK in my case, because with that a hoster of our service could change his setup to use only one filesystem which supports atomic moves, as that's what we expect in the system requirements anyway. What I don't want to deal with is things silently failing just because an implementation uses a copy-delete-strategy which may result in data corruption in the target file. So, from my understanding it is simply not safe to rely on Files.move for atomic operations, because it doesn't always fail if those are not supported, but implementations may fall back to a copy-delete-strategy.</p>
  
  <p>Is such behaviour a bug in the implementation and needs to get filed or does the documentation allow such behaviour and I'm understanding it wrong? Does it make any difference at all if I now already know that such maybe broken implementations are used out there? I would need to synchronize the write access on my own in that case...</p>