Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>No, canWrite is not suitable for this purpose. In general the file will be writable even if another process is writing.</p> <p>You need a higher level protocol to coordinate the locking. If you plan to use this code on a single platform, you may be able to use <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/nio/channels/FileLock.html" rel="noreferrer">NIO's FileLock facility</a>. But read the documentation carefully, and note that on many platforms, the lock is only advisory.</p> <p>Another approach is to have one process write the file with a name that your process won't recognize, then rename the file to a recognizable name when the write is complete. On most platforms, the rename operation is atomic if the source and destination are the same file system volume. The name change might use a different file extension, or even moving the file from one directory to another (on the same volume).</p> <p>Since in this case you are working exclusively with XML, looking for a close tag would work, but it isn't foolproof&mdash;what if there are comments after the final markup, or the writer or simply doesn't write valid XML?</p> <p>Looking for the EOF will <em>not</em> work. There will always be an EOF, even when the writer has just opened the file and hasn't written anything yet. If this weren't so, the easiest thing would be to allow the reader to start parsing as soon as the file showed up; it would simply block until the writer closed the file. But the file system doesn't work this way. Every file has an end, even if some process is currently moving it.</p>
 

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