Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between java.io.PrintWriter and java.io.BufferedWriter?

Please look through code below:

// A.class File file = new File("blah.txt"); FileWriter fileWriter = new FileWriter(file); PrintWriter printWriter = new PrintWriter(fileWriter);  // B.class File file = new File("blah.txt"); FileWriter fileWriter = new FileWriter(file); BufferedWriter bWriter = new BufferedWriter(fileWriter); 

What is the difference between these two methods?

When should we use PrintWriter over BufferedWriter?

like image 378
i2ijeya Avatar asked Nov 17 '09 06:11

i2ijeya


People also ask

What is Java IO PrintWriter?

The Java PrintWriter class ( java. io. PrintWriter ) enables you to write formatted data to an underlying Writer . For instance, writing int , long and other primitive data formatted as text, rather than as their byte values.

What is Java BufferedWriter?

BufferedWriter is a sub class of java. io. Writer class. BufferedWriter writes text to character output stream, buffering characters so as to provide for the efficient writing of single characters, arrays, and strings. The buffer size may be specified, or the default size may be used.

What is the difference between PrintWriter and FileWriter?

PrintWriter gives you some handy methods for formatting like println and printf . So if you need to write printed text - you can use it. FileWriter is more like "low-level" writer that gives you ability to write only strings and char arrays.

What is the difference between PrintWriter and PrintStream?

PrintStream and PrintWriter have nearly identical methods. The primary difference is that PrintStream writes raw bytes in the machine's native character format, and PrintWriter converts bytes to recognized encoding schemes.


2 Answers

PrintWriter gives more methods (println), but the most important (and worrying) difference to be aware of is that it swallows exceptions.

You can call checkError later on to see whether any errors have occurred, but typically you'd use PrintWriter for things like writing to the console - or in "quick 'n dirty" apps where you don't want to be bothered by exceptions (and where long-term reliability isn't an issue).

I'm not sure why the "extra formatting abilities" and "don't swallow exceptions" aspects are bundled into the same class - formatting is obviously useful in many places where you don't want exceptions to be swallowed. It would be nice to see BufferedWriter get the same abilities at some point...

like image 61
Jon Skeet Avatar answered Sep 17 '22 16:09

Jon Skeet


The API reference for BufferedWriter and PrintWriter detail the differences.

The main reason to use the PrintWriter is to get access to the printXXX methods like println(). You can essentially use a PrintWriter to write to a file just like you would use System.out to write to the console.

A BufferedWriter is an efficient way to write to a file (or anything else), as it will buffer the characters in Java memory before (probably, depending on the implementation) dropping to C to do the writing to the file.

There is no such concept as a "PrintReader"; the closest you will get is probably java.util.Scanner.

like image 25
TofuBeer Avatar answered Sep 18 '22 16:09

TofuBeer