Java独占写文件的5种解决方法

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>

参考自StackOverflow上的某个问题以及csdn上的这篇博客: 以独占方式访问某个文件
包含自己的一些理解
// 方案1:利用RandomAccessFile的文件操作选项s,s即表示同步锁方式写
RandomAccessFile file = new RandomAccessFile(file, "rws");

// 方案2:利用FileChannel的文件锁
File file = new File("test.txt");
FileInputStream fis = new FileInputStream(file);
FileChannel channel = fis.getChannel();
FileLock fileLock = null;
while(true) {
   fileLock = channel.tryLock(0, Long.MAX_VALUE, false);  // true表示是共享锁,false则是独享锁定
   if(fileLock!=null) break;
   else   // 有其他线程占据锁
      sleep(1000);
}

// 方案3:先将要写的内容写入一个临时文件,然后再将临时文件改名(Hack方案,利用了缓冲+原子操作的原理)
public class MyFile {
   private String fileName;
   public MyFile(String fileName) {
      this.fileName = fileName;
   }

   public synchronized void writeData(String data) throws IOException {
      String tmpFileName = UUID.randomUUID().toString()+".tmp";
      File tmpFile = new File(tmpFileName);
      FileWriter fw = new FileWriter(tmpFile);
      fw.write(data);
      fw.flush();
      fw.close();

      // now rename temp file to desired name, this operation is  atomic operation under most os
      if(!tmpFile.renameTo(fileName) {
         // we may want to retry if move fails
         throw new IOException("Move failed");
      }
   }
}

// 方案4:根据文件路径封装文件,并且用synchronized控制写文件
public class MyFile {
   private String fileName;
   public MyFile(String fileName) {
      this.fileName = fileName;
   } 
   public synchronized void writeData(String data) throws IOException {
      FileWriter fw = new FileWriter(fileName);
      fw.write(data);
      fw.flush();
      fw.close();
   }         
}

// 方案5:我自己想出来的一个方案,不太精确。通过切换设置读写权限控制,模拟设置一个可写标记量(蜕变成操作系统中经典的读写问题....)
public class MyFile {
   private volatile boolean canWrite = true;
   private String fileName;
   public MyFile(String fileName) {
      this.fileName = fileName;
   }
   public void writeData(String data) {
      while(!canWrite) {
          try { Thread.sleep(100); } catch(InteruptedException ie) {  }  // 可以设置一个超时写时间
      }
      canWrite = false;
      
      // Now write file

      canWrite = true;
   }
}