Possible duplicate:
How to lock a file using java (if possible)
I have two processes that invoke two Java programs that modify the same text file. I noticed that the contents of the text file are missing data. I suspect that when one Java program receives a stream of writes to a text file, I think that it blocks another java program from modifying it (for example, when you have a file open, you cannot delete this file). Is there any way around this other than the database? (not to say that the db solution is not clean or elegant, we just wrote a lot of codes to manipulate this text file)
EDIT
It turns out that I made a mistake aimed at the problem. The reason the data in my text file is missing is because
ProcessA : Add Data Rows to a Text File
ProcessB : First load all the lines of the text field in the List . Then it processes the list of this list. At the end of ProcessB write a list, replacing the contents of the text file.
This work is great in a consistent process. But when sharing, if ProcessA adding data to a file, during ProcessB manipulating the List , then when ProcessB writes List , regardless of what only ProcessA just adds, it will be overridden, Therefore, my initial thought was before ProcessB to write List back, synchronize data between text file and List . Therefore, when I write List back, it will contain everything. so here is my effort
public void synchronizeFile(){ try { File file = new File("path/to/file/that/both/A/and/B/write/to"); FileChannel channel = new RandomAccessFile(file, "rw").getChannel(); FileLock lock = channel.lock();
So logList is the current list that ProcessB wants to write. Therefore, before writing, I read the file and saved the data in tempList , if tempList and logList do not match, synchronize them. The problem is that at the moment, both ProcessA and ProcessB are currently accessing the file, so when I try to lock the file and read from it List<PackageLog> tempList = readAllLogs(file); , I either get an OverlappingFileLockException or java.io.IOException: The process cannot access the file because another process has locked a portion of the file . Please help me solve this problem :(
EDIT2 : I understand Lock
public static void main(String[] args){ File file = new File("C:\\dev\\harry\\data.txt"); FileReader fileReader = null; BufferedReader bufferedReader = null; FileChannel channel = null; FileLock lock = null; try{ channel = new RandomAccessFile(file, "rw").getChannel(); lock = channel.lock(); fileReader = new FileReader(file); bufferedReader = new BufferedReader(fileReader); String data; while((data = bufferedReader.readLine()) != null){ System.out.println(data); } }catch(IOException e){ e.printStackTrace(); }finally{ try { lock.release(); channel.close(); if(bufferedReader != null) bufferedReader.close(); if(fileReader != null) fileReader.close(); } catch (IOException e) { e.printStackTrace(); } } }
and I got this error IOException: The process cannot access the file because another process has locked a portion of the file