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.

Chapter 1. Introducing I/O<br />

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

Input and output, I/O for short, are fundamental to any computer operating system or<br />

programming language. Only theorists find it interesting to write programs that don't require<br />

input or produce output. At the same time, I/O hardly qualifies as one of the more "thrilling"<br />

topics in computer science. It's something in the background, something you use every day—<br />

but for most developers, it's not a topic with much sex appeal.<br />

There are plenty of reasons for <strong>Java</strong> programmers to find I/O interesting. <strong>Java</strong> includes a<br />

particularly rich set of I/O classes in the core API, mostly in the java.io package. For the<br />

most part I/O in <strong>Java</strong> is divided into two types: byte- and number-oriented I/O, which is<br />

handled by input and output streams; and character and text I/O, which is handled by readers<br />

and writers. Both types provide an abstraction for external data sources and targets that allows<br />

you to read from and write to them, regardless of the exact type of the source. You use the<br />

same methods to read from a file that you do to read from the console or from a network<br />

connection.<br />

But that's just the tip of the iceberg. Once you've defined abstractions that let you read or<br />

write without caring where your data is coming from or where it's going to, you can do a lot<br />

of very powerful things. You can define I/O streams that automatically compress, encrypt,<br />

and filter from one data format to another, and more. Once you have these tools, programs can<br />

send encrypted data or write zip files with almost no knowledge of what they're doing;<br />

cryptography or compression can be isolated in a few lines of code that say, "Oh yes, make<br />

this an encrypted output stream."<br />

In this book, I'll take a thorough look at all parts of <strong>Java</strong>'s I/O facilities. This includes all the<br />

different kinds of streams you can use. We're also going to investigate <strong>Java</strong>'s support for<br />

Unicode (the standard multilingual character set). We'll look at <strong>Java</strong>'s powerful facilities for<br />

formatting I/O—oddly enough, not part of the java.io package proper. (We'll see the reasons<br />

for this design decision later.) Finally, we'll take a brief look at the <strong>Java</strong> Communications API<br />

(javax.comm), which provides the ability to do low-level I/O through a computer's serial and<br />

parallel ports.<br />

I won't go so far as to say, "If you've always found I/O boring, this is the book for you!" I will<br />

say that if you do find I/O uninteresting, you probably don't know as much about it as you<br />

should. I/O is the means for communication between software and the outside world<br />

(including both humans and other machines). <strong>Java</strong> provides a powerful and flexible set of<br />

tools for doing this crucial part of the job.<br />

Having said that, let's start with the basics.<br />

1.1 What Is a Stream?<br />

A stream is an ordered sequence of bytes of undetermined length. Input streams move bytes<br />

of data into a <strong>Java</strong> program from some generally external source. Output streams move bytes<br />

of data from <strong>Java</strong> to some generally external target. (In special cases streams can also move<br />

bytes from one part of a <strong>Java</strong> program to another.)<br />

14

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

Saved successfully!

Ooh no, something went wrong!