[Forward] CREATING A File Lock ON A File Using JDK1.4

xiaoxiao2021-03-06  97

The behavior of the file lock is platform-dependent. On some platforms, the file lock is advisory, which means that unless an application checks for a file lock, it will not be prevented from accessing the file. On other platforms, the file lock IS Mandatory, Which Means That A File Lock Prevents Any Application from Accessing The File.

Try {

// Get a file channel for the file

File File = New File ("FileName");

Filechannel Channel = New RandomaccessFile (File, "RW"). GetChannel ();

// Use the file channel to create a lock on the file.

// this Method Blocks Until It Can Retrieve The Lock.

FileLock Lock = Channel.lock ();

// Try Acquiring The Lock WITHOUT BLOCKING. THIS METHOD RETURNS

// null or throws an exception if the file is already Locked.

Try {

Lock = CHANNEL.TRYLOCK ();

} catch (overlappingfilelockexception e) {

// File is already Locked in this thread or virtual machine

}

// Release the Lock

Lock.release ();

// Close the file

Channel.Close ();

} catch (exception e) {

}

转载请注明原文地址:https://www.9cbs.com/read-123323.html

New Post(0)