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.