Search code examples
javafile-ioread-write

Java - allow access to files during I/O


Is there any way to allow access to files while they are being used in Java?

BufferedWriter bw = new BufferedWriter(new FileWriter(file));
BufferedReader br = new BufferedReader(new FileReader(file));
String data = "";
String line;
while((line = br.readLine()) != null)
{
    data += line;
}
bw.write(data + msg);
br.close();
bw.close();

Otherwise, I need to do something like that each time I want to add a single line to a text file...

Thanks!

To clarify: I need external programs to be able to access the file while Java is writing to it. I do NOT want to use the code above each time I need to add a line to the file. I do want to open a filewriter from the start, and append whenever I need to (but as I said, I need to allow external programs access, so this won't work!).


Solution

  • As you are under Windows there is no solution. It is Windows that is blocking concurrent access to the file, not Java.