advertisement
Login | Register   
  Include Code  Search Tips
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   TIP BANK
Browse DevX
Download the code for this article
Partners & Affiliates
advertisement
advertisement
advertisement
advertisement
 

Enable Cross-platform File Locking with a Lock Server

A custom shared lock server can overcome file-locking differences between operating systems or Java implementations.  


advertisement
JDK 1.4 adds a number of advanced features related to I/O and filesystems. The more advanced I/O features are often non-portable or only partially portable. File locking is one of these features. While most operating systems provide some form of file locking, file locking isn't implemented exactly the same way on every platform.

For a general example of file locking in Java, review the following code snippet. It locks the first 10 bytes of a file called "foo.txt":


RandomAccessFile raf =
new RandomAccessFile( "foo.txt", "rw" );
FileChannel fc = raf.getChannel();
FileLock lock = fc.lock( 0, 10, false );

It's quick, easy and you get access to all the articles on DevX.
This registration/login is to allow you to read articles on devx.com.
Already a member?



advertisement