Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Locking a file  RSS feed

 
kasi nandini
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there any mechanism through which we can lock a file opened using FileInputStream. that is if i am accessing one file called properties file via some program. no other program should be given permission to access that properties file until the program which was using it exits.
Note: I want this to be done in JDK 1.3 only
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moved to the IO forum.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In 1.3? Not really. You want the FileLock that finally showed up in 1.4. Perhaps you could do something like this: move the file to someplace no other applications are likely to access it. Do whatever processing you need to, and when you're done, move the file back to where it started. The biggest risk here is, what if your program dies while the file is in the alternate directory? Even with really robust error handling, you can't prevent, say, a power loss. So you need a recovery process of some sort. Maybe whenever your program starts, it checks for properties files in its private directory that don't exist in the dir the props file is supposed to be in. If something's still in the private directory that was never moved back to the main dir, that tells you an error occurred in the last run. Well, there are any number of ways to do this; just make sure you do something to handle the case of an error preventing the file from being moved back where it should be.
It may also be useful to leave behind a copy of the file you're working with, but change its name (e.g. by appending "_backup") or move it to a nearby backup directory. This might make it easier for others to recover the original file if they need to.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!