Here you can find the source of lockFile(File file, RandomAccessFile raf)
public static FileLock lockFile(File file, RandomAccessFile raf) throws IOException
//package com.java2s; //License from project: Open Source License import java.io.*; import java.nio.channels.*; public class Main { public static FileLock lockFile(File file, RandomAccessFile raf) throws IOException { FileLock fl = null;//from ww w . ja v a 2 s . c o m FileChannel fc = raf.getChannel(); if (fc != null) { try { fl = fc.tryLock(); } catch (OverlappingFileLockException ex) { throw new IOException( file.getPath() + ":\nDatei ist gesperrt.\n" + "Bitte schlie\u00DFen Sie die Datei in dem Programm,\n" + "in dem sie ge\u00F6ffnet ist."); } catch (IOException ex) { throw new IOException(file.getPath() + ":\nDatei kann nicht gesperrt werden."); } } return fl; } }