Java文件锁定

java文件锁定一般都通过FileChannel来实现。主要涉及如下2个方法:

  • tryLock() throws IOException  试图获取对此通道的文件的独占锁定。
  • tryLock(long position, long size, boolean shared) throws IOException  试图获取对此通道的文件给定区域的锁定。
  1 import java.io.*;
  2 import java.nio.channels.*;
  3 
  4 public class LockFileDemo {
  5 
  6     public static void main(String[] args) throws Exception {
  7         String filePath = "c:\\test.txt";
  8 
  9         File file = new File(filePath);
 10         RandomAccessFile raf = new RandomAccessFile(file, "rw");
 11         FileChannel fc = raf.getChannel();
 12         FileLock fl = fc.tryLock();
 13 
 14         if (fl.isValid()) {
 15             System.out.println("Get the lock successed!");
 16 
 17             // 测试读线程
 18             ReadFile rf = new ReadFile(file);
 19             rf.start();
 20 
 21             // 测试写线程
 22             // WriteFile wf = new WriteFile(file,"This is a test!----幻影");
 23             // wf.start();
 24 
 25         }
 26 
 27         fl.release();
 28         System.out.println("release the lock!");
 29         raf.close();
 30     }
 31 }
 32 
 33 /****
 34  * 写文件
 35  * 
 36  * @author Administrator
 37  * 
 38  */
 39 class WriteFile extends Thread {
 40     File file;
 41     String context;
 42 
 43     public WriteFile() {
 44 
 45     }
 46 
 47     public WriteFile(File file, String context) {
 48         this.file = file;
 49         this.context = context;
 50     }
 51 
 52     public void run() {
 53         FileWriter fw = null;
 54 
 55         try {
 56             fw = new FileWriter(file);
 57             fw.write(context);
 58             fw.flush();
 59         } catch (IOException e) {
 60             e.printStackTrace();
 61         }
 62     }
 63 }
 64 
 65 /****
 66  * 读文件
 67  * 
 68  * @author Administrator
 69  * 
 70  */
 71 class ReadFile extends Thread {
 72     File file;
 73 
 74     public ReadFile() {
 75     }
 76 
 77     public ReadFile(File file) {
 78         this.file = file;
 79     }
 80 
 81     public void run() {
 82         FileReader fr = null;
 83         try {
 84             fr = new FileReader(file);
 85 
 86             int c;
 87             System.out.println("----------开始文件读取----------");
 88             while ((c = fr.read()) != -1) {
 89                 System.out.print((char) c);
 90             }
 91             System.out.println("");
 92             System.out.println("----------文件读取完毕----------");
 93         } catch (FileNotFoundException e) {
 94             System.out.println("文件不存在!");
 95         } catch (IOException e) {
 96             e.printStackTrace();
 97         } finally {
 98             if (fr != null) {
 99                 try {
100                     fr.close();
101                 } catch (IOException e) {
102                     e.printStackTrace();
103                 }
104             }
105         }
106     }
107 }

程序输出:

Get the lock successed!
release the lock!
----------开始文件读取----------
This is a test!----幻影
----------文件读取完毕----------

tryLock等同于tryLock(0L, Long.MAX_VALUE, false) ,它获取的是独占锁,所以一定是在释放锁之后,才能读取到文件内容。

也就是说,上例中,一定会先打印“release the lock!”,然后再打印出文件内容。

 

将上面的代码改成(共享锁):

FileLock fl = fc.tryLock(0, file.length(), true);//共享锁

.......


Thread.sleep(2000);//为了区分清楚点
fl.release();
System.out.println("release the lock!");
raf.close();

那么才可能先打印出文件内容,然后打印“release the lock!”

 

posted @ 2013-03-31 17:50  yejg1212  阅读(5761)  评论(0编辑  收藏  举报