Yes, otherwise some data might not reach the output.
Detail: The close()
method of PrintWriter
does not throw an IOException
, so it can be outside of any try
block.
PrintWriter
Yet another advantage of PrintWriter
is that
you can ask it to automatically flush its data every time a
println()
is executed.
This ensures that data reaches its destination.
(But it is still a good precaution to close()
the stream when you are through with it.)
Here is a list of some of the constructors and methods
used with PrintWriter
.
For a complete list of methods refer to
the Java documentation.
None of the methods throw exceptions.
Constructors
PrintWriter(Writer out) Create a new PrintWriter and connect it to the Writer out. Automatic line flushing is not enabled. PrintWriter(Writer out, boolean autoFlush) Create a new PrintWriter and connect it to the Writer out. Automatic line flushing is enabled if autoFlush is true.
Methods
public boolean checkError() Flush the stream. If there has been an error in output the method returns true. public void close() Flush the stream and close the file. public void flush() Flush the stream. public void print(char c) Print the character neld in c. public void print(double d) Translate into characters and print the double value d. public void print(float f) Translate into characters and print the float value f. public void print(int i) Translate into characters and print the int value i. public void print(String s) Print the String s. public void print( various other data types ) See your complete documentation. public void println() Terminate the current output line and (possibly) flush the stream. public void println( various data types ) See Java on-line documentation.
Can you pretty much print
any type of data you want?