获得文件锁

//获得文件锁

import java.io.FileOutputStream;
import java.nio.channels.*;
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();
  }
}

你可能感兴趣的:(exception,import,string,class,null,file,J2SE基础)