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 word stream is derived from an analogy with a stream of water. An input stream is like a<br />

siphon that sucks up water; an output stream is like a hose that sprays out water. Siphons can<br />

be connected to hoses to move water from one place to another. Sometimes a siphon may run<br />

out of water if it's drawing from a finite source like a bucket. On the other hand, if the siphon<br />

is drawing water from a river, it may well provide water indefinitely. So too an input stream<br />

may read from a finite source of bytes like a file or an unlimited source of bytes like<br />

System.in. Similarly an output stream may have a definite number of bytes to output or an<br />

indefinite number of bytes.<br />

Input to a <strong>Java</strong> program can come from many sources. Output can go to many different kinds<br />

of destinations. The power of the stream metaphor and in turn the stream classes is that the<br />

differences between these sources and destinations are abstracted away. All input and output<br />

are simply treated as streams.<br />

1.1.1 Where Do Streams Come From?<br />

The first source of input most programmers encounter is System.in. This is the same thing as<br />

stdin in C, generally some sort of console window, probably the one in which the <strong>Java</strong><br />

program was launched. If input is redirected so the program reads from a file, then System.in<br />

is changed as well. For instance, on Unix, the following command redirects stdin so that<br />

when the MessageServer program reads from System.in, the actual data comes from the file<br />

data.txt instead of the console:<br />

% java MessageServer < data.txt<br />

The console is also available for output through the static field out in the java.lang.System<br />

class, that is, System.out. This is equivalent to stdout in C parlance and may be redirected<br />

in a similar fashion. Finally, stderr is available as System.err. This is most commonly used<br />

for debugging and printing error messages from inside catch clauses. For example:<br />

try {<br />

//... do something that might throw an exception<br />

}<br />

catch (Exception e) { System.err.println(e); }<br />

Both System.out and System.err are print streams, that is, instances of<br />

java.io.PrintStream.<br />

Files are another common source of input and destination for output. File input streams<br />

provide a stream of data that starts with the first byte in a file and finishes with the last byte in<br />

the file. File output streams write data into a file, either by erasing the file's contents and<br />

starting from the beginning or by appending data to the file. These will be introduced in<br />

Chapter 4.<br />

Network connections provide streams too. When you connect to a web server or FTP server<br />

or something else, you read the data it sends from an input stream connected from that server<br />

and write data onto an output stream connected to that server. These streams will be<br />

introduced in Chapter 5.<br />

15

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

Saved successfully!

Ooh no, something went wrong!