NIO读写文件并加锁
Posted 鼠标
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了NIO读写文件并加锁相关的知识,希望对你有一定的参考价值。
一、读取文件
1 package lock; 2 3 import java.io.File; 4 import java.io.FileNotFoundException; 5 import java.io.IOException; 6 import java.io.RandomAccessFile; 7 import java.nio.channels.FileChannel; 8 import java.nio.channels.FileLock; 9 import java.util.Calendar; 10 11 public class ReadFileLock implements Runnable { 12 13 public void run() { 14 try { 15 Calendar calstart = Calendar.getInstance(); 16 Thread.sleep(5000); 17 File file = new File("D:/test.txt"); 18 // 给该文件加锁 19 RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw"); 20 FileChannel fileChannel = randomAccessFile.getChannel(); 21 FileLock fileLock = null; 22 while (true) { 23 try { 24 fileLock = fileChannel.tryLock(); 25 break; 26 } catch (Exception e) { 27 System.out.println("有其他线程正在操作该文件,当前线程" + Thread.currentThread().getName() + "休眠1000毫秒"); 28 Thread.sleep(1000); 29 } 30 } 31 byte[] buf = new byte[1024]; 32 StringBuffer sb = new StringBuffer(); 33 while ((randomAccessFile.read(buf)) != -1) { 34 sb.append(new String(buf, "utf-8")); 35 buf = new byte[850]; 36 } 37 38 System.err.println(sb.toString()); 39 40 fileLock.release(); 41 fileChannel.close(); 42 randomAccessFile.close(); 43 randomAccessFile = null; 44 45 Calendar calend = Calendar.getInstance(); 46 System.out.println("当前线程:" + Thread.currentThread().getName() + ",读文件共花了" 47 + (calend.getTimeInMillis() - calstart.getTimeInMillis()) + "秒"); 48 } catch (FileNotFoundException e) { 49 e.printStackTrace(); 50 } catch (IOException e) { 51 e.printStackTrace(); 52 } catch (InterruptedException e) { 53 e.printStackTrace(); 54 } 55 } 56 57 public static void main(String[] args) { 58 ReadFileLock threadTarget = new ReadFileLock(); 59 Thread read = new Thread(threadTarget); 60 read.setName("thread-read-file"); 61 read.start(); 62 Thread read2 = new Thread(threadTarget); 63 read2.setName("thread-write-file2"); 64 read2.start(); 65 } 66 67 }
二、写文件
package lock; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.io.RandomAccessFile; import java.nio.channels.FileChannel; import java.nio.channels.FileLock; import java.util.Calendar; public class ReadFileLock implements Runnable { public void run() { try { Calendar calstart = Calendar.getInstance(); Thread.sleep(5000); File file = new File("D:/test.txt"); // 给该文件加锁 RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw"); FileChannel fileChannel = randomAccessFile.getChannel(); FileLock fileLock = null; while (true) { try { fileLock = fileChannel.tryLock(); break; } catch (Exception e) { System.out.println("有其他线程正在操作该文件,当前线程" + Thread.currentThread().getName() + "休眠1000毫秒"); Thread.sleep(1000); } } byte[] buf = new byte[1024]; StringBuffer sb = new StringBuffer(); while ((randomAccessFile.read(buf)) != -1) { sb.append(new String(buf, "utf-8")); buf = new byte[850]; } System.err.println(sb.toString()); fileLock.release(); fileChannel.close(); randomAccessFile.close(); randomAccessFile = null; Calendar calend = Calendar.getInstance(); System.out.println("当前线程:" + Thread.currentThread().getName() + ",读文件共花了" + (calend.getTimeInMillis() - calstart.getTimeInMillis()) + "秒"); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } } public static void main(String[] args) { ReadFileLock threadTarget = new ReadFileLock(); Thread read = new Thread(threadTarget); read.setName("thread-read-file"); read.start(); Thread read2 = new Thread(threadTarget); read2.setName("thread-write-file2"); read2.start(); } }
以上是关于NIO读写文件并加锁的主要内容,如果未能解决你的问题,请参考以下文章
java大文件读写操作,java nio 之MappedByteBuffer,高效文件/内存映射
JavaNIO的深入研究4内存映射文件I/O,大文件读写操作,Java nio之MappedByteBuffer,高效文件/内存映射