0


I have some corruption problems with property files. What I suspect is that concurrent reading/writing is the problem. So I want to put some file locks.
First I have looked at this: How can I lock a file using java (if possible)
So I can put locks on write, but not on read, which does not solve my problem. Then I have looked at this: Java: how to handle two process trying to modify the same file
So if I use RandomAccessFile, I can put lock on read AND write, which seems correct. So my remaining problem is simple: load a Property with RandomAccessFile. But I see no simple way to have a Reader or an InputStream from a RandomAccessFile. Is there one?
Thanks,
Guillaume

Edit: As there is some ambiguity, I have to add that the concurrency is due to multiple processes accessing the file, not multithreading

Community
  • 1
  • 1
  • Check [this out](http://stackoverflow.com/questions/9287664/why-cant-a-randomaccessfile-be-casted-to-inputstream) – Stefan Lindner Mar 06 '17 at 14:14
  • 1
    The lock for write does solve your problem. It will exclude all other writers but also all other readers (it is called a write lock, but more accurately is an *exclusive* lock). – john16384 Mar 06 '17 at 14:16
  • @john16384, how does that work? Does the OS prevent other processes from opening the file? Is that guaranteed by Java? – Guillaume Guigue Mar 06 '17 at 14:19
  • If the only program accessing this property file is your program, you don’t need file locks; you can just use synchronization, locks, or one of the many classes in the java.util.concurrent package. – VGR Mar 06 '17 at 14:20
  • If you use NIO FileLock, then yes, the OS guarantees it. Read its docs under the heading "Platform dependencies`. – john16384 Mar 06 '17 at 14:29

0 Answers0