// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.channels.FileLock;

public class Lock {
    public static OutputStream uselessLock(File f) throws IOException {
        return new FileOutputStream(f, true);
    }
    public static FileLock workingLock(File f) throws IOException {
        return new FileOutputStream(f, true).getChannel().lock();
    }
    public static void main(String[] args) throws Exception {
        File f = new File("C:\\z.z");
        f.createNewFile();
        //uselessLock(f);
        workingLock(f);
        OutputStream os = new FileOutputStream(f, true);
        os.write(123);
        os.close();
     }
}
