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 />

operations from moving bytes from one device to another. <strong>Java</strong> is the first major language to<br />

recognize and take advantage of this.<br />

Organization of the Book<br />

This book has 17 chapters that are divided into four parts, plus two appendixes.<br />

Part I: Basic I/O<br />

Chapter 1<br />

Chapter 2<br />

Chapter 3<br />

Chapter 1 introduces the basic architecture and design of the java.io package,<br />

including the reader/stream dichotomy. Some basic preliminaries about the int, byte,<br />

and char data types are discussed. The <strong>IO</strong>Exception thrown by many I/O methods is<br />

introduced. The console is introduced, along with some stern warnings about its<br />

proper use. Finally, I offer a cautionary message about how the security manager can<br />

interfere with most kinds of I/O, sometimes in unexpected ways.<br />

Chapter 2 teaches you the basic methods of the java.io.OutputStream class you<br />

need to write data onto any output stream. You'll learn about the three overloaded<br />

versions of write(), as well as flush() and close(). You'll see several examples,<br />

including a simple subclass of OutputStream that acts like /dev/null and a TextArea<br />

component that gets its data from an output stream.<br />

The third chapter introduces the basic methods of the java.io.InputStream class<br />

you need to read data from a variety of sources. You'll learn about the three<br />

overloaded variants of the read() method and when to use each. You'll see how to<br />

skip over data and check how much data is available, as well as how to place a<br />

bookmark in an input stream, then reset back to that point. You'll learn how and why<br />

to close input streams. This will all be drawn together with a StreamCopier program<br />

that copies data read from an input stream onto an output stream. This program will be<br />

used repeatedly over the next several chapters.<br />

Part II: Data Sources<br />

Chapter 4<br />

The majority of I/O involves reading or writing files. Chapter 4 introduces the<br />

FileInputStream and FileOutputStream classes, concrete subclasses of<br />

InputStream and OutputStream that let you read and write files. These classes have<br />

all the usual methods of their superclasses, such as read(), write(), available(),<br />

flush(), and so on. Also in this chapter, development of a File Viewer program<br />

commences. You'll see how to inspect the raw bytes in a file in both decimal and<br />

hexadecimal format. This example will be progressively expanded throughout the rest<br />

of the book.<br />

3

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

Saved successfully!

Ooh no, something went wrong!