Ok so I am learning about I/O, and I found the following code in one of the slides. can someone please explain why there is a need to have a FileWrite, BufferedWriter and PrintWriter? I know BufferedWriter is to buffer the output and put it all at once but why would they use FileWriter and PrintWriter ? dont they pretty much do the same with a bit of difference in error handling etc?
And also why do they pass bw
to PrintWriter
?
FileWriter fw = new FileWriter (file);
BufferedWriter bw = new BufferedWriter (fw);
PrintWriter outFile = new PrintWriter (bw);
FileWriter writes directly into Files and should be used only when the number of writes is less. BufferedWriter: BufferedWriter is almost similar to FileWriter but it uses internal buffer to write data into File. So if the number of write operations is more, the actual IO operations are less and performance is better.
The biggest difference is that the print and println methods of PrintWriter take arguments of any type, generally calling the toString() or String. valueOf() methods to get String objects. The BufferedWriter write() method takes a single character, an array of characters, or a String.
Using PrintWriter PrintWriter implements all of the print() methods found in PrintStream , so we can use all formats which you use with System. out. println() statements. To append content to an existing file, open the writer in append mode by passing the second argument as true .
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.
Presumably they're using a FileWriter
because they want to write to a file. Both BufferedWriter
and PrintWriter
have to be given another writer to write to - you need some eventual destination.
(Personally I don't like FileWriter
as it doesn't let you specify the encoding. I prefer to use FileOutputStream
wrapped in an OutputStreamWriter
, but that's a different matter.)
BufferedWriter
is used for buffering, as you say - although it doesn't buffer all the output, just a fixed amount of it (the size of the buffer). It creates "chunkier" writes to the underlying writer.
As for the use of PrintWriter
- well, that exposes some extra methods such as println
. Personally I dislike it as it swallows exceptions (you have to check explicitly with checkError
, which still doesn't give the details and which I don't think I've ever seen used), but again it depends on what you're doing. The PrintWriter
is passed the BufferedWriter
as its destination.
So the code below the section you've shown will presumably write to the PrintWriter
, which will write to the BufferedWriter
, which will (when its buffer is full, or it's flushed or closed) write to the FileWriter
, which will in turn convert the character data into bytes on disk.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With