?? filelocktest.java
字號:
import java.nio.*;
import java.nio.channels.*;
import java.io.*;
/**
* Description:
* <br/>Copyright (C), 2008-2010, Yeeku.H.Lee
* <br/>This program is protected by copyright laws.
* <br/>Program Name:
* <br/>Date:
* @author Yeeku.H.Lee kongyeeku@163.com
* @version 1.0
*/
public class FileLockTest
{
public static void main(String[] args)
{
FileChannel channel = null;
try
{
//使用FileOutputStream獲取FileChannel
channel = new FileOutputStream("a.txt")
.getChannel();
//使用非阻塞式方式對指定文件加鎖
FileLock lock = channel.tryLock();
//程序暫停5s
Thread.sleep(5000);
//釋放鎖
lock.release();
}
catch (Exception ex)
{
ex.printStackTrace();
}
finally
{
try
{
if (channel != null)
channel.close();
}
catch (IOException ex)
{
ex.printStackTrace();
}
}
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -