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);
Usually, this kind of Writer chaining is about abstraction.
PrintWriter
have some usefulprint
andprintln
methods that can be convenient if you want to print Strings and lines to a File. Working directly withFileWriter
, you would have to use a more "low level" API. And as you sayBufferedWriter
is about buffering. So it's basically a matter of what you want to output to the file, and what level of abstraction you prefer.PrintWriter from here
from the above statement it seems the main reason to use PrintWriter is to get the access of all the methods of
PrintStream
likeprintln()
,println(char [] x)
etc.BufferedWriter, You are right It's one of the best way to write to a file because it will buffered the character into the virtual memory before writing to a file directly and came up with a
newLine()
method.FileWriter from here
.
FileWriter
is simply to write plain text(without formatting) it doesn't use any buffer mechanism, whatever comes its way it just writes.BufferedWriter
is a wrapper forWriter
classes to allow it to be able to use buffer functionality (to optimize IO).PrintWriter
prints formatted text, you can provide format string along with the data to be printed, though it can directly work with anyWriter/OutputStream
, just to provide buffering,Writer/OutputStream
is 1st passed toBufferedWriter
then to have formatted text is passed toPrintWriter
From the Docs:
You can understand from this that a
BufferedWriter
is an efficient way to write stuff.A
FileWriter
object is passed to theBufferedWriter
as the intent here is to write to some output file using aBufferedWriter
.And finally, a
PrintWriter
is used forprint*
methods likeprintln()
.Presumably they're using a
FileWriter
because they want to write to a file. BothBufferedWriter
andPrintWriter
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 useFileOutputStream
wrapped in anOutputStreamWriter
, 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 asprintln
. Personally I dislike it as it swallows exceptions (you have to check explicitly withcheckError
, 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. ThePrintWriter
is passed theBufferedWriter
as its destination.So the code below the section you've shown will presumably write to the
PrintWriter
, which will write to theBufferedWriter
, which will (when its buffer is full, or it's flushed or closed) write to theFileWriter
, which will in turn convert the character data into bytes on disk.The objects are wrapped in this order because you want to use the outermost PrintWriter for its more sophisticated formatting. BufferedWriter must be wrapped on something. So FileWriter, as a result, is what BufferedWriter wraps and is the innermost object.