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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

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

The read() and available() methods work exactly as with normal input streams. However,<br />

they first attempt to read from the pushback buffer.<br />

public int read() throws <strong>IO</strong>Exception<br />

public int read(byte[] data, int offset, int length) throws <strong>IO</strong>Exception<br />

public int available() throws <strong>IO</strong>Exception<br />

6.5 Print Streams<br />

System.out and System.err are instances of the java.io.PrintStream class. This is a<br />

subclass of FilterOutputStream that converts numbers and objects to text. System.out is<br />

primarily used for simple, character-mode applications and for debugging. Its raison d'être is<br />

convenience, not robustness; print streams ignore many issues involved in internationalization<br />

and error checking. This makes System.out easy to use in quick and dirty hacks and simple<br />

examples, while simultaneously making it unsuitable for production code, which should use<br />

the java.io.PrintWriter class (discussed in Chapter 15) instead.<br />

The PrintStream class has print() and println() methods that handle every <strong>Java</strong> data<br />

type. The print() and println() methods differ only in that println() prints a platformspecific<br />

line terminator after printing its arguments and print() does not. These methods are:<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[] s)<br />

public void print(String s)<br />

public void print(Object o)<br />

public void println()<br />

public void println(boolean b)<br />

public void println(char c)<br />

public void println(int i)<br />

public void println(long l)<br />

public void println(float f)<br />

public void println(double d)<br />

public void println(char[] s)<br />

public void println(String s)<br />

public void println(Object o)<br />

Anything at all can be passed to a print() method; whatever argument you give is<br />

guaranteed to match at least one of these methods. Object types are converted to strings by<br />

invoking their toString() method. Primitive types are converted with the appropriate<br />

String.valueOf() method.<br />

One aspect of making System.out simple for quick jobs is not in the PrintStream class at<br />

all but in the compiler. Because <strong>Java</strong> overloads the + operator to signify concatenation of<br />

strings, primitive data types, and objects, you can pass multiple variables to the print() and<br />

println() methods, which are then converted to strings and concatenated . For example,<br />

consider the line:<br />

System.out.println("As of " + (new Date()) + " there have been over "<br />

+ hits + " hits on the web site." );<br />

84

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

Saved successfully!

Ooh no, something went wrong!