{"id":6976,"date":"2014-05-16T01:17:54","date_gmt":"2014-05-16T01:17:54","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/05\/16\/java-file-locking-on-a-network-collection-of-common-programming-errors\/"},"modified":"2014-05-16T01:17:54","modified_gmt":"2014-05-16T01:17:54","slug":"java-file-locking-on-a-network-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/05\/16\/java-file-locking-on-a-network-collection-of-common-programming-errors\/","title":{"rendered":"Java file locking on a network-Collection of common programming errors"},"content":{"rendered":"<p>This is perhaps similar to previous posts, but I want to be specific about the use of locking on a network, rather than locally. I want to write a file to a shared location, so it may well go on a network (certainly a Windows network, maybe Mac). I want to prevent other people from reading any part of this file whilst it it being written. This will not be a highly concurrent process, and the files will be typically less than 10MB.<\/p>\n<p>I&#8217;ve read the <code>FileLock<\/code> documentation and <code>File<\/code> documentation and am left somewhat confused, as to what is safe and what is not. I want to lock the entire file, rather than portions of it.<\/p>\n<p>Can I use <code>FileChannel.tryLock()<\/code>, and it is safe on a network, or does it depend on the type of network? Will it work on a standard Windows network (if there is such a thing).<\/p>\n<p>If this does not work, is the best thing to create a zero byte file or directory as a lock file, and then write out the main file. Why does that <code>File.createNewFile()<\/code> documentation say don&#8217;t use this for file locking? I appreciate this is subject to race conditions, and is not ideal.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is perhaps similar to previous posts, but I want to be specific about the use of locking on a network, rather than locally. I want to write a file to a shared location, so it may well go on a network (certainly a Windows network, maybe Mac). I want to prevent other people from [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-6976","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/6976","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=6976"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/6976\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=6976"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=6976"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=6976"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}