• Post Reply Bookmark Topic Watch Topic
  • New Topic

Lock RandomAccessFile for writing

 
learner
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to write to a file and while its being written I do not want it to be read. After the file has been fully written and closed , it should be available for reading. How may I do this ? If I use the FileLock facility with the RandomAccessFile, then that does not seem to work. I am able to read as well as write to the file. Using FileLock on FileOutputStream does not work also. I can read the file using FileInputStream.

Thanks
 
Sunny Kumar
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can try to use the synchronise option with the block thats writing to the file..so that no other thread is allowed to read from it.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
FileLock is designed to prevent other applications from accessing a locked file (usually), however it may have no effect at all in blocking access from other threads within the same JVM instance. On some systems it may block both, but the API indicates that FileLock is not designed for preventing access within the same JVM. So probably you need to do both: use a FileLock to block external access, and use synchronization to block internal access.

Note that both synchronization and (to a lesser extent) FileLock provide onsensual protection - that is, they protect you from other threads or programs that use the same protocol for protection. If someone writes code for another thred which does not use any synchronization, for example, then that thread will simply ignore your attempt to protect the file with synchronization. So it's the programmer's responsibility to ensure that all the code which might possibly access the same file from within the same JVM instance, is synchronized the same way. With FileLock, we're really relying on the operating system to do that for us. On most systems I'm used, FileLock works well in preventing other programs from accessing the locked file. But it's not guaranteed - some other programs may be written in a way that essentially ignores file locks. And some operatuing systems might not enforce them at all. So you can't guarantee the file is completely safe, but you can give it a pretty good chance of being safe. Test on your own system to see how well it works.

Another option is to use File.renameTo() to move the entire file to an alternate (secret, private) location while you're working on it. Then when you're done, move it back to its original directory. This is probably the most reliabel way to prevent other apps from accessing the file simultaneously - though you have to consider error handling very carefully. What should happen if an error occurs during processing? Is it OK if the file never gets moved back to where it came from? Remember that no even if you use a finally block to ensure that the renameTo() is exacuted in the end no matter what, there are some events that can prevent the program from being able do this. For example, if the computer loses electrical power while processing. Unlikely but possible - so if the data in the file is really important, you may need to plan for this sort of situation.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!