开发者

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 开发者_StackOverflow社区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!).


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


In the concepts of java.nio you're able to non-blocking file access.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜