|
-
How to use Java to lock a file
My Java program is packaged as an .exe and will run on a Web server. If more
than one user is utilizing our Web site, more than one instance of this .exe
file might be running concurrently.
The program needs to read information from some files on the Web server.
The Java code that opens and reads the files does not lock the files. I'll
need to lock the files while they are being read so that two instances of
the .exe don't try to read the same file at the same time.
Here is the code that opens the file, reads it, and closes it:
String metricsFilePath = "MyMetricsFile.afm";
File metricsFile = null;
FileReader In = null;
try
{
metricsFile = new File(metricsFilePath);
In = new FileReader(metricsFilePath);
... Read the file ...
}
catch(FileNotFoundException e)
{
throw new FileNotFoundException("\r\nCan't find file \"" + metricsFilePath
+ "\", which is the font-metrics file for the " + fontName + " font");
}
catch(IOException e)
{
throw new IOException("\r\nCan't read file \"" + metricsFilePath + "\",
which is the font-metrics file for the " + fontName + " font");
}
finally
{
if(In != null) {In.close();}
}
Can somebody tell me how to enhance this code so that it locks and unlocks
the file?
Thanks.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks