Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

can we open multiple FileWriter stream to the same file at the same time?

Tags:

java

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.

like image 352
Thang Pham Avatar asked Jun 13 '11 19:06

Thang Pham


People also ask

Will it be possible to access multiple files using a single stream justify?

No, you cannot. An instance of FileInputStream can only be used once with one file.

Can you read and write to the same file in Java?

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.


1 Answers

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.

like image 199
Vineet Reynolds Avatar answered Oct 01 '22 23:10

Vineet Reynolds