Summary of Class PrintWriter

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.
go to previous page   go to home page   go to next page        

Answer:

Yes, especially when buffers are used you should flush pending output before the program ends.

Summary of Class PrintWriter

Yet another advantage of PrintWriter is that you can ask it to automatically flush its data everytime 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 your on-line 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() throws IOException
    Flush the stream and close the file.

public void flush() throws IOException
    Flush the stream.

public void print(char c)
    Print the character 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 your complete documentation.

QUESTION 16:

Can you pretty much print any type of data you want?