Completion requirements
This chapter explains Java's FileWriter class and how you can use it to store data in files.
16. Summary of Class PrintWriter
Answer:
Yes, especially when buffers are used you should flush pending output before the program ends.
Detail: The close()
method of PrintWriter
does not throw an IOException
, so it can be outside of any try
block.
Summary of Class PrintWriter
Summary of Class PrintWriter
Yet another advantage of PrintWriter
is that you can ask it to automatically flush its data every time a println()
is executed. (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.
Question 16:
Can you pretty much