File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Read/Write file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Read/Write file" Watch "Read/Write file" New topic
Author

Read/Write file

Mike Landis
Greenhorn

Joined: Jun 05, 2003
Posts: 21
Hello,
I have a situation where multiple application
instances are reading and writing (maybe) to same file simultaneously. JDK is version 1.3.x and in my case there is no database available.
How should I handle possible file corruptions?
Any patterns/techniques available?
mike
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1821

In this situation I generally create a "lock file", which is basically an empty file in the same directory as the file I want to access. The file name is the same as the name of the file I want to access with a "_" prepended to it.
My code then tries to create this file. If it receives an I/O error stating that the file already exists then it continues looping. Otherwise it creates the file, does what it needs to the source file, and then deletes the lock file.
Of course, there are several design issues with this:
What if the application stops while the lock file is there and the lock file does not get removed? Someone would have to manually remove that lock file.
Do you have permissions to create a file in the directory? Usually the answer is "yes," but theoretically you might not.
It's not a perfect design, but then again, not having a database is not a perfect situation


Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Mike Landis
Greenhorn

Joined: Jun 05, 2003
Posts: 21
Ok, thank you!
I also try this approach.
Br
Mike
Originally posted by Joel McNary:
In this situation I generally create a "lock file", which is basically an empty file in the same directory as the file I want to access. The file name is the same as the name of the file I want to access with a "_" prepended to it.
My code then tries to create this file. If it receives an I/O error stating that the file already exists then it continues looping. Otherwise it creates the file, does what it needs to the source file, and then deletes the lock file.
Of course, there are several design issues with this:
What if the application stops while the lock file is there and the lock file does not get removed? Someone would have to manually remove that lock file.
Do you have permissions to create a file in the directory? Usually the answer is "yes," but theoretically you might not.
It's not a perfect design, but then again, not having a database is not a perfect situation
 
Consider Paul's rocket mass heater.
 
subject: Read/Write file