// : c12:filelocking.java // {clean: file.txt} // from 'thinking in java, 3rd ed.' (c) bruce eckel 2002 // www.bruceeckel.com. see copyright notice in copyright.txt.
import java.io.fileoutputstream; import java.nio.channels.filelock;
public class filelocking { public static void main(string[] args) throws exception { fileoutputstream fos = new fileoutputstream("file.txt"); filelock fl = fos.getchannel().trylock(); if (fl != null) { system.out.println("locked file"); thread.sleep(100); fl.release(); system.out.println("released lock"); } fos.close(); } } ///:~
|