• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

FileLock on Windows

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a question regarding file locking on Windows and the Java API for FileLock (I tried this on both Windows XP and Windows Server 2003). Let's assume I have two programs (different JVMs possibly on different machines) that access the same file (test.txt)- one for reading and the other for writing. In an effort to do things correctly, the reading program is going to try and get a shared lock prior to reading. Conversely, the writing program will get an exclusive lock prior to writing.

Here is sample code from the reading program:



Here is the sample code from the writing program:




If the reading program is run first, it obtains the lock and then sleeps indefinitely. I then run the writing program and it blocks attempting to obtain the exclusive lock, which I would expect. However, by executing the first line successfully (new FileOutputStream("test.txt")), it has already overwritten the file. Interestingly, if you remove the logic to obtain the lock from the writing program, you get an exception at fos.write() line stating that the file is locked by another process. This is great but, again, the file has already been truncated to 0 bytes by invoking new FileOutputStream().

It seems strange to me, especially on Windows, that it would prevent you from writing to the file but allow you to completely overwrite the file. Any information anyone could provide would be appreciated. It is actually a little difficult to code around this because in order to get a FileLock you need and FileChannel, and in order to get a FileChannel you need a FileOutputStream. However, I can't get a FileOutputStream without overwriting the file. I realize I can use the RandomAccessFile to get a FileChannel but due to existing code, I need to get a FileOutputStream eventually.

Thanks in advance,
Ned
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could try creating a FileOutputStream in append mode (so it will not truncate the file) and then set the position to 0, but I haven't tested if that will work.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And welcome to JavaRanch
reply
    Bookmark Topic Watch Topic
  • New Topic