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.

Chapter 4. File Streams<br />

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

Until now, most of the examples in this book have used the streams System.in and<br />

System.out. These are convenient for examples, but in real life, you'll more commonly attach<br />

streams to data sources like files and network connections. You'll use the<br />

java.io.FileInputStream and java.io.FileOutputStream classes, which are concrete<br />

subclasses of java.io.InputStream and java.io.OutputStream, to read and write files.<br />

FileInputStream and FileOutputStream provide input and output streams that let you read<br />

and write files. We'll discuss these classes in detail in this chapter; they provide the standard<br />

methods for reading and writing data. What they don't provide is a mechanism for filespecific<br />

operations, like finding out whether a file is readable or writable. For that, you may<br />

want to look forward to Chapter 12, which talks about the File class itself and the way <strong>Java</strong><br />

works with files.<br />

4.1 Reading Files<br />

java.io.FileInputStream is a concrete subclass of java.io.InputStream. It provides an<br />

input stream connected to a particular file.<br />

public class FileInputStream extends InputStream<br />

FileInputStream has all the usual methods of input streams, such as read(), available(),<br />

skip(), and close(), which are used exactly as they are for any other input stream.<br />

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

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

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

public native long skip(long n) throws <strong>IO</strong>Exception<br />

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

public native void close() throws <strong>IO</strong>Exception<br />

These methods are all implemented in native code, except for the two multibyte read()<br />

methods. These, however, just pass their arguments on to a private native method called<br />

readBytes(), so effectively all these methods are implemented with native code. (In <strong>Java</strong> 2,<br />

read(byte[] data, int offset, int length) is a native method that read(byte[] data)<br />

invokes.)<br />

There are three FileInputStream() constructors, which differ only in how the file to be read<br />

is specified:<br />

public FileInputStream(String fileName) throws <strong>IO</strong>Exception<br />

public FileInputStream(File file) throws FileNotFoundException<br />

public FileInputStream(FileDescriptor fdObj)<br />

The first constructor uses a string containing the name of the file. The second constructor uses<br />

a java.io.File object. The third constructor uses a java.io.FileDescriptor object.<br />

Filenames are platform-dependent, so hardcoded file names should be avoided where<br />

possible. Using the first constructor violates Sun's rules for "100% Pure <strong>Java</strong>" immediately.<br />

Therefore, the second two constructors are much preferred. Nonetheless, the second two will<br />

have to wait until File objects and file descriptors are discussed in Chapter 12. For now, I<br />

will use only the first.<br />

51

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

Saved successfully!

Ooh no, something went wrong!