3

As per the Java SE 8 Documentation, the BufferedWriter class has the following methods of its own(w.r.t writing data):

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. 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?

Nilashish C
  • 375
  • 2
  • 11

2 Answers2

6

write(String str) in Writer calls write(String str, int off, int len), which is overridden in BufferedWriter. So your data will be buffered.

khelwood
  • 55,782
  • 14
  • 81
  • 108
2

Reasoning about which method BufferedWriter overrides is pointless. 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?).

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. 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.

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. So in the best case, the data in larger than the buffer and you have just unnecessarily created a BufferedWriter instance. 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.

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. Further, you should use the try-with-resources construct to safely close the 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

Holger
  • 285,553
  • 42
  • 434
  • 765