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!”
标签:
java
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 周边上新:园子的第一款马克杯温暖上架
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
· 使用C#创建一个MCP客户端