5種解決Java獨(dú)占寫文件的方法
本文實(shí)例講解了5種解決Java獨(dú)占寫文件的方法,包含自己的一些理解,如若有不妥的地方歡迎大家提出。
方案1:利用RandomAccessFile的文件操作選項(xiàng)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則是獨(dú)享鎖定
if(fileLock!=null) break;
else // 有其他線程占據(jù)鎖
sleep(1000);
}
方案3:先將要寫的內(nèi)容寫入一個(gè)臨時(shí)文件,然后再將臨時(shí)文件改名(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:根據(jù)文件路徑封裝文件,并且用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:我自己想出來的一個(gè)方案,不太精確。通過切換設(shè)置讀寫權(quán)限控制,模擬設(shè)置一個(gè)可寫標(biāo)記量(蛻變成操作系統(tǒng)中經(jīng)典的讀寫問題....)
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) { } // 可以設(shè)置一個(gè)超時(shí)寫時(shí)間
}
canWrite = false;
// Now write file
canWrite = true;
}
}
以上就是Java獨(dú)占寫文件的解決方法,大家有沒有學(xué)會(huì),可以參考其他文章進(jìn)行學(xué)習(xí)理解。
相關(guān)文章
Assert.assertNotNull()斷言是否是空問題
這篇文章主要介紹了Assert.assertNotNull()斷言是否是空問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-10-10
關(guān)于訪問后端接口報(bào)404錯(cuò)誤問題的解決方法(全網(wǎng)最細(xì)!)
404頁面的出現(xiàn)會(huì)降低用戶體驗(yàn),那么導(dǎo)致404頁面出現(xiàn)的原因是什么呢?這篇文章主要給大家介紹了關(guān)于訪問后端接口報(bào)404錯(cuò)誤問題的解決方法,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-04-04
SpringBoot集成Tomcat服務(wù)架構(gòu)配置
這篇文章主要為大家介紹了SpringBoot集成Tomcat服務(wù)架構(gòu)配置,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02
使用Springboot封裝一個(gè)自適配的數(shù)據(jù)單位轉(zhuǎn)換工具類
我們?cè)诮邮涨芭_(tái)傳輸?shù)臄?shù)據(jù)時(shí),往往SpringBoot使用內(nèi)置的數(shù)據(jù)類型轉(zhuǎn)換器把我們提交的數(shù)據(jù)自動(dòng)封裝成對(duì)象等類型,下面這篇文章主要給大家介紹了關(guān)于使用Springboot封裝一個(gè)自適配的數(shù)據(jù)單位轉(zhuǎn)換工具類的相關(guān)資料,需要的朋友可以參考下2023-03-03
使用chatgpt實(shí)現(xiàn)微信聊天小程序的代碼示例
這篇文章主要介紹了使用chatgpt實(shí)現(xiàn)微信聊天小程序(秒回復(fù)),文中有詳細(xì)的代碼示例,對(duì)大家了解chatgpt聊天有一定的幫助,感興趣的同學(xué)可以參考閱讀2023-05-05
如何基于SpringMVC實(shí)現(xiàn)斷點(diǎn)續(xù)傳(HTTP)
這篇文章主要介紹了如何基于SpringMVC實(shí)現(xiàn)斷點(diǎn)續(xù)傳(HTTP),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-01-01

