简体   繁体   English

BufferedWriter的write(String S)方法实际上是缓冲区吗?

[英]Does BufferedWriter's write(String S) method actually buffer?

As per the Java SE 8 Documentation , the BufferedWriter class has the following methods of its own(wrt writing data): 根据Java SE 8 Documentation ,BufferedWriter类有自己的以下方法(写入数据):

write(char[] cbuf, int off, int len)
write(int c)
write(String s, int off, int len)

As I confirmed from checking the source code of this class, it does not override Writer's write(String s) method. 正如我通过检查此类的源代码确认的那样,它不会覆盖Writer的write(String s)方法。 It simply inherits it. 它只是继承它。

My question is, given the following code: 我的问题是,给出以下代码:

public static void SaveTextToFile(String fileName, String data, boolean append) throws IOException {
        BufferedWriter bw = new BufferedWriter(new FileWriter(new File(fileName)));
        bw.write(data);
        bw.close();
    }

Will the data be actually buffered before writing to a file? 在写入文件之前,数据是否会被实际缓冲? If not, in which scenarios does the buffering occur? 如果不是,在哪些情况下缓冲发生?

write(String str) in Writer calls write(String str, int off, int len) , which is overridden in BufferedWriter . write(String str)Writer调用write(String str, int off, int len) ,它在BufferedWriter重写。 So your data will be buffered. 所以你的数据将被缓冲。

Reasoning about which method BufferedWriter overrides is pointless. 推理BufferedWriter覆盖哪种方法毫无意义。 All non-abstract methods of the Writer class are implemented in terms of other methods, to finally end up at one of the abstract methods, as the Writer itself does not implement any actual writing logic (how could it?). Writer类的所有非抽象方法都是根据其他方法实现的,最终以一种抽象方法结束,因为Writer本身并没有实现任何实际的写入逻辑(怎么可能呢?)。

Since Writer does not have any notion of a delegation target, it's impossible to have any method delegating to the target writer without interception by the BufferedWriter subclass, as only that subclass knows the target. 由于Writer没有任何委托目标的概念,因此只有该子类知道目标,才有可能在没有BufferedWriter子类拦截的情况下将任何方法委托给目标Writer器。 So all write methods, as well as the append methods, are under the BufferedWriter 's control and will buffer, if the the data to be written is smaller than the buffer's capacity. 因此,如果要写入的数据小于缓冲区的容量,则所有write方法以及append方法都在BufferedWriter的控制之下并将BufferedWriter

That said, in your example, 那就是说,在你的例子中,

BufferedWriter bw = new BufferedWriter(new FileWriter(new File(fileName)));
bw.write(data);
bw.close();

there is no advantage in buffering, as the buffer will be flushed right in the subsequent close() operation. 缓冲没有优势,因为缓冲区将在后续的close()操作中被刷新。 So in the best case, the data in larger than the buffer and you have just unnecessarily created a BufferedWriter instance. 所以在最好的情况下, data大于缓冲区,你只是不必要地创建了一个BufferedWriter实例。 But if data is smaller than the buffer, you have also performed an unnecessary copy operation from data to the buffer, before the data gets actually written. 但是,如果data小于缓冲区,则在实际写入数据之前,还会从data到缓冲区执行不必要的复制操作。

For writing a single item, there is no sense in buffering. 对于写一个项目,缓冲是没有意义的。 Besides that, it is quiet dangerous to offer an append parameter that is actually ignored, pretending a functionality that is not there, especially when this can result in unintended overwriting of existing data in the target file. 除此之外,提供一个实际被忽略的append参数是非常危险的,假装一个不存在的功能,特别是当这会导致意外覆盖目标文件中的现有数据时。 Further, you should use the try-with-resources construct to safely close the writer: 此外,您应该使用try-with-resources构造来安全地关闭writer:

public static void SaveTextToFile(String fileName, String data, boolean append)
                                                                throws IOException {
    try(Writer w = new FileWriter(fileName, append)) {
        w.write(data);
    }
}

or 要么

// use StandardOpenOption.APPEND to append
public static void SaveTextToFile(String fileName, String data, OpenOption... o)
                                                                throws IOException {
    Files.write(Paths.get(fileName),
                Collections.singleton(data), Charset.defaultCharset(), o);
}

which may render your method obsolete, as it only delegates to an already existing method 这可能会使您的方法过时,因为它只委托给现有的方法

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

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