File IO: Reading and Writing Stream Data

     

File IO in Java can be very confusing at first. The reason is because there are a number of abstract classes, and a large number of IO classes that implement specific aspects of IO behavior or requirements, which results in the programmer wrapping two or often three different File class constructors in order to get the desired implementation.

The classes we refer to here are in the java.io package.

Input and Output Streams

Java's implementation of input and output (I/O) services is based on streams. Streams are used to read and write byte data. The data may come from a file, a network socket, a remote object, a serialized object, input at the command line, or somewhere else. Streams can be filtered, allowing limited encryption, serialization of object data, compression using the zip or other algorithm, translation, and so on.

InputStream and OutputStream are the two abstract parent classes that other, more specialized stream types subclass to do the actual work of reading and writing byte data.

InputStream

Depending on what the source of your bytes is, you use a different subclass. Dig: FileInputStream gets bytes from a file. Meant for reading raw bytes. For reading character data, try FileReader.

ByteArrayInputStream

Contains an internal buffer that keeps track of the next byte to be supplied.

PipedInputStream

Gets bytes from an object of type PipedOutputStream. This is intended for use in multithreaded programming, where data is read from a PipedInputStream object by one thread, and the data is written to the PipedOutputStream on another thread.

OutputStream

Depending on where you want to write your bytes, you use one of the following implementing subclasses:

FileOutputStream

Writes the bytes directly to a File object or a FileDescriptor. Use this one if you have byte data like images to write. It's not the best choice for character data ”for that consider FileWriter. Note that some platforms may allow a file to be written to by only one stream at a time.

ByteArrayOutputStream

Implements an output stream that writes data to an array of bytes, allowing the buffer to automatically grow. Get the data written to this object using toString() or toByteArray() .

PipedOutputStream

This is the sender of data in a piped relationship.

Characters and PrintStream

There are two reasons to refrain from using byte input and output streams for reading and writing character data.

The first is that Java uses Unicode to represent every character with 2 bytes. The purpose of doing so was to ease and encourage internationalization and localization with languages whose characters (such as Korean and Chinese) require 2 bytes. Many plain text files from which you'll read data in, however, use only one byte (8 bits) to represent each character in ASCII text.

The second is that when you work with files containing text, you need to be able to read and write line by line; basic stream I/O doesn't have the concept.

However, you can work with character data in streams by using the PrintStream class. PrintStream provides a number of print and println methods that mimic system calls that are familiar to programmers of C and C++. A chief benefit is that it automatically converts characters, which allows you to pass Strings, chars, and other primitives as arguments. Although it may sound foreign at this point, PrintStream is actually the IO implementation that you're most familiar with; System.out is a print stream, wired for standard output. System.err is a PrintStream as well.

An advantage to working with a PrintStream is that it can be set to automatically flush the buffer after a byte array is written, one of the print methods is called, or if a newline character ('\n' ) is written to the stream. This accounts for the behavior of System.in, which is implemented as an InputStream.

The unusual thing about working with PrintStream is that it does not throw IOException . Instead, a flag is set internally that can be checked using the checkError() method.

Note that when you use a PrintStream, it converts all characters to bytes, using the current platform's underlying character encoding. If you need to write characters instead of bytes, use a PrintWriter.



Java Garage
Java Garage
ISBN: 0321246233
EAN: 2147483647
Year: 2006
Pages: 228
Authors: Eben Hewitt

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net