Can we open multiple FileWriter stream to the same file at the same time. I wrote some codes to test this, and apparently it allow to be happened. This stumble me. Since if I open a File Writer to file, and before close it, I try to delete the file, well I cant. So how and why can I open multiples FileWriter stream to the same file at one time? Here is what I try
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 exact same thing, but instead write 2
out, and I get both 1
and 2
, in my data file.
No, you cannot. An instance of FileInputStream can only be used once with one file.
You can't open the same file to read and write at the same time. You have to open and save the file information in a data structure and then close it. Then you have to work with the data structure in memory and open the file to write results. And when you finish to write you should close it.
Can we open multiple FileWriter stream to the same file at the same time.
Yes, it is quite possible.
So how and why can I open multiples FileWriter stream to the same file at one time?
You've already demonstrated how to open multiple FileWriter instances, so I'll stick to why it is possible. In Java, all file or device based operations are typically dependent on the platform's capabilities. You can consider java.io
and other related packages to be thin wrappers around native code in the JVM that actually performs this functionality.
Until Java 1.4 (when NIO came out), file locking was not possible in the Java, because the JVM did not make appropriate platform-specific system calls to lock files or ranges within files. This changed with NIO, which is available in the java.nio
package. In the documentation of the FileChannel class, you'll notice the following:
In addition to the familiar read, write, and close operations of byte channels, this class defines the following file-specific operations:
...
A region of a file may be locked against access by other programs.
This behavior, as you would have correctly guessed is due to the necessary platform-specific calls being made by the JVM. If the underlying platform does not support this, file locking will not occur.
As to why the behavior exists with FileWriter, the reason is very simple. NIO is/was the set of new I/O classes, but it did not replace java.io
. You could therefore continue to use the java.io
classes like FileOutputStream and FileWriter, but you'll never be able to have the files locked by the JVM for the duration of the write operation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With