23.07.2013 Views

Java IO.pdf - Nguyen Dang Binh

Java IO.pdf - Nguyen Dang Binh

Java IO.pdf - Nguyen Dang Binh

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

There are four constructors in this class:<br />

public PrintWriter(Writer out)<br />

public PrintWriter(Writer out, boolean autoFlush)<br />

public PrintWriter(OutputStream out)<br />

public PrintWriter(OutputStream out, boolean autoFlush)<br />

<strong>Java</strong> I/O<br />

The PrintWriter can send text either to an output stream or to another writer. If autoFlush<br />

is set to true, the PrintWriter is flushed every time println() is invoked.<br />

The PrintWriter class implements the abstract write() method from java.io.Writer and<br />

overrides five other methods:<br />

public void write(int c)<br />

public void write(char[] text)<br />

public void write(String s)<br />

public void write(String s, int offset, int length)<br />

public void flush()<br />

public void close()<br />

These methods are used almost identically to their equivalents in any other Writer class. The<br />

one difference is that none of them throw <strong>IO</strong>Exceptions; in fact, no method in the<br />

PrintWriter class ever throws an <strong>IO</strong>Exception. If the underlying output stream or writer<br />

throws an <strong>IO</strong>Exception, it's caught inside PrintWriter and an error flag is set. Read the<br />

status of this flag with the checkError() method:<br />

public boolean checkError()<br />

Since checkError() returns a boolean, it only tells you that an I/O error has occurred; it<br />

does not tell you what that error was. Furthermore, once an error has occurred, checkError()<br />

always returns true—there is no way to reset it so you can test for later errors. On the other<br />

hand, you can indicate that an error has occurred with setError():<br />

protected void setError()<br />

The main advantages of the PrintWriter class are the nine-way overloaded print() method<br />

and the 10-way overloaded println() method. Any <strong>Java</strong> object, variable, or literal can be<br />

printed by passing it to a print() or println() method. The println() method follows its<br />

argument with a platform-dependent line separator (such as \r\n) and then flushes the output<br />

if autoFlush is enabled. The print() method does not. Otherwise, these methods are the<br />

same.<br />

public void print(boolean b)<br />

public void print(char c)<br />

public void print(int i)<br />

public void print(long l)<br />

public void print(float f)<br />

public void print(double d)<br />

public void print(char[] text)<br />

public void print(String s)<br />

public void print(Object obj)<br />

public void println()<br />

public void println(boolean b)<br />

public void println(char c)<br />

379

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!