Writing Text Files
13. The BufferedWriter Stream
Answer:
The close()
method will throw an exception, which will be caught. The catch
block will write an error message that is not
really correct. You could fix the error message, or add another try/catch
structure to the program that deals only with closing the file.
The BufferedWriter
Stream
BufferedWriter
Stream
Disk input and output is more efficient when a buffer is used. For our small example programs it doesn't matter much. However, programs that do extensive I/O should use buffers. Use BufferedWriter to do this with a character output stream.
BufferedWriter(Writer out)
Construct a buffered character-output stream
Since a FileWriter
is a Writer
it is the correct type for the parameter. Here is how this might be done:
BufferedWriter out
= new BufferedWriter(new FileWriter("stuff.txt"));
Question 13:
Do all operating systems end lines of text in the same way?