简体   繁体   English

我们可以同时打开多个 FileWriter stream 到同一个文件吗?

[英]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.我们可以同时打开多个 FileWriter stream 到同一个文件。 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?那么如何以及为什么我可以一次打开多个 FileWriter stream 到同一个文件? 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.我有另一个进程,它做同样的事情,但是写出2 ,我在我的数据文件中得到12

Can we open multiple FileWriter stream to the same file at the same time.我们可以同时打开多个 FileWriter stream 到同一个文件。

Yes, it is quite possible.是的,这很有可能。

So how and why can I open multiples FileWriter stream to the same file at one time?那么如何以及为什么我可以一次打开多个 FileWriter stream 到同一个文件?

You've already demonstrated how to open multiple FileWriter instances, so I'll stick to why it is possible.您已经演示了如何打开多个 FileWriter 实例,所以我将坚持为什么它是可能的。 In Java, all file or device based operations are typically dependent on the platform's capabilities.在 Java 中,所有基于文件或设备的操作通常取决于平台的功能。 You can consider java.io and other related packages to be thin wrappers around native code in the JVM that actually performs this functionality.您可以将java.io和其他相关包视为围绕实际执行此功能的 JVM 中的本机代码的瘦包装器。

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.在 Java 1.4 之前(当 NIO 出现时),文件锁定在 Java 中是不可能的,因为 JVM 没有在适当的平台内调用系统调用文件来锁定文件。 This changed with NIO, which is available in the java.nio package. NIO 改变了这种情况,它在java.nio package 中可用。 In the documentation of the FileChannel class, you'll notice the following:FileChannel class 的文档中,您会注意到以下内容:

In addition to the familiar read, write, and close operations of byte channels, this class defines the following file-specific operations:除了熟悉的字节通道的读、写和关闭操作之外,这个 class 还定义了以下文件特定的操作:

... ...

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.正如您已经正确猜到的那样,这种行为是由于 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.至于为什么 FileWriter 存在这种行为,原因很简单。 NIO is/was the set of new I/O classes, but it did not replace java.io . NIO 是/曾经是一组新的 I/O 类,但它没有取代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.因此,您可以继续使用java.io类,如 FileOutputStream 和 FileWriter,但您将永远无法在写操作的持续时间内被 Z18B5A217C4DAD25662D3A05EDB0E39 锁定文件。

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. FileWriter API 文档说该行为取决于平台,并且文档中没有任何内容表明当您想要对文件进行排他锁定时应该使用 FileWriter。

You should probably look at and consider using FileChannel instead.您可能应该查看并考虑改用FileChannel That class has proper support for locking files and preventing two threads writing to the same file at the same time. class 对锁定文件和防止两个线程同时写入同一个文件有适当的支持。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 是否可以使用相同的FileWriter文件并通过多种方法对其进行写入? - Is is possible use the same FileWriter file and write to it from multiple methods? 多次关闭并打开同一文件错误 - Multiple times stream close and open on same file error 多个Java进程可以同时读取同一个文件吗? - Can multiple Java processes read the same file at the same time? Java &gt; 8 流同时计算多个项目 - Java > 8 stream count multiple items at the same time 我们可以同时从多个线程访问同一个实例的同步方法和非同步方法吗? - Can we Access Synchronized method and an unsynchronized method of same instance from multiple threads at the same time? 同时打开多个插座 - Having multiple sockets open at the same time 我们如何同时执行多个 function 或者我们可以在 AWS lambda 的环境变量中使用多个 function - How we can execute multiple function at same time or we can use multiple function in environmental variables in AWS lambda 多个线程可以同时将数据写入文件吗? - Can multiple threads write data into a file at the same time? 我可以在Java的同一类中编写FileWriter和bufferedwriter吗? - Can I write FileWriter and bufferedwriter in a same class in Java? 如何在插入数据库的过程中同时启动和休眠多个线程 - how can we start and sleep multiple thread at a same time during inserting in database
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM