If the Java™ program allows other programs access to a file at the same time, the Java program can lock bytes in the file for a period of time. During that time, the program has exclusive use of that section of the file.
When a lock is successful, the integrated file system classes return an IFSKey object. This object is supplied to the unlock() method to indicate which bytes to unlock. When the file is closed, the system unlocks all locks that are still on the file (the system does an unlock for every lock that the program did not unlock).
The following example shows how to use the IFSKey class.
// Create an AS400 object. AS400 sys = new AS400("mySystem.myCompany.com"); // Open an input stream. This // constructor opens with share_all // so other programs can open this // file. IFSFileInputStream aFile = new IFSFileInputStream(sys,"/mydir1/mydir2/myfile"); // Lock the first 1K bytes in the // file. Now no other instance can // read these bytes. IFSKey key = aFile.lock(1024); // Read the first 1K of the file. byte data[] = new byte[1024]; aFile.read(data); // Unlock the bytes of the file. aFile.unlock(key); // Close the file. aFile.close();