简体   繁体   中英

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

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.

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.

The FileWriter API docs say the behaviour is platform dependent, and there's nothing to in the documentation that indicates FileWriter should be used when you want an exclusive lock on a file.

You should probably look at and consider using FileChannel instead. That class has proper support for locking files and preventing two threads writing to the same file at the same time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM