Can we open multiple FileWriter streams in the same file at the same time?

Is it possible to open multiple FileWriter streams in the same file at the same time. I wrote some codes to test this, and apparently this allows to happen. It stumbles. Because if I open a Writer file for a file, and before closing it, I try to delete the file, well, I can’t. So, how and why can I open multiple FileWriter streams in the same file at a time? This is what i am trying

private static final int SIZE = 1000; public static void main(String[] args) throws IOException, InterruptedException { File file = new File("C:\\dev\\harry\\data.txt"); FileWriter fileWriter = new FileWriter(file, true); BufferedWriter bufferedWriter = new BufferedWriter(fileWriter); for (int i = 0; i < SIZE; i++) { bufferedWriter.write("1\n"); Thread.sleep(100); } if (bufferedWriter != null) bufferedWriter.close(); if (fileWriter != null) fileWriter.close(); } 

I have another process that does the same thing, but instead I write 2 out, and I get both 1 and 2 in my data file.

+4
source share
2 answers

Is it possible to open multiple FileWriter streams in the same file at the same time.

Yes, it is quite possible.

So, how and why can I open multiple FileWriter streams in the same file at a time?

You have already demonstrated how to open multiple instances of FileWriter, so I will stick to why this is possible. In Java, all file or device-based operations typically depend on the capabilities of the platform. You can consider java.io and other related packages as thin shells around the native code in the JVM, which actually performs this function.

Before Java 1.4 (when NIO came out), file locking was not possible in Java because the JVM did not create the appropriate system calls to block files or ranges within files. This has been modified using NIO, which is available in the java.nio package. In the documentation for the FileChannel class , you will notice the following:

In addition to the familiar read, write, and close operations of byte channels, this class defines the following file operations:

...

The file area may be locked to access other programs.

This behavior, you guessed it right, is due to the necessary platform-specific calls made by the JVM. If the underlying platform does not support this, file locking will not occur.

Regarding why the behavior exists with FileWriter, the reason is very simple. NIO is a collection of new I / O classes, but it has not replaced java.io Therefore, you could continue to use java.io classes such as FileOutputStream and FileWriter, but you can never lock JVM files for the entire duration of the write operation.

+4
source

The FileWriter API docs say that the behavior is platform-specific, and in the documentation that states that FileWriter should be used if you want to use exclusive file locking, there should be nothing in it.

You should probably look and think about using FileChannel . This class has proper support for file locking and prevents the simultaneous writing of two streams to the same file.

+2
source

All Articles