General Concepts


Java uses stream-based I/O. A data stream is simply a flow of data from a data source to a destination. An input stream connects a data destination to a source. Data is read from the stream. An output stream connects a data source to a destination. Data is written to the stream. The data source might be an array of data, a String , the contents of a file, or input received from the keyboard. Possible destinations include the console, a file, a Java program, or even another data stream.

As you might expect, I/O streams are encapsulated in classes, largely found in the java.io package. To open a stream, you create an instance of the appropriate I/O class. The stream object will invoke methods to read or write data to its stream. There are two general types of Java I/O streams ”byte and character. Byte streams are used to read and write byte data, either 1 byte at a time or an array of bytes. Byte streams are useful for I/O operations on things like images or sound files. Byte streams can also be used to save and restore objects.

One of the most common I/O tasks is to either read data from a file or write data to a file. Using a byte stream to read or write character data is a bit inconvenient. The data has to be first read as byte data, then converted into character data. In addition, byte streams can only read 8-bit bytes, meaning they cannot read all Unicode characters. To make things easier for you and to provide I/O capability for all Unicode characters, Java provides a family of character I/O streams that read or write 16-bit character data. These classes allow you to read and write character data directly as characters or Strings .

The Java I/O libraries provide classes that allow buffered I/O for improved performance. Rather than reading or writing each byte or character individually, the data is first stored in an internal buffer. When the buffer reaches its capacity, its contents are read in or written out. Buffering improves efficiency by reducing the number of low-level system calls required for the read or write. This efficiency gain is particularly important when reading or writing to a file. There are also classes that define streams that are wrapped around other streams to add additional functionality to the underlying stream. For example, an InputStreamReader can be wrapped around the standard input stream, allowing you to read keyboard input as character data.

The next few sections are meant to provide a brief overview of the I/O classes and methods that you will most likely use in your scientific and engineering programming work. It is not meant to be comprehensive and won't discuss every class and method in detail. The Java I/O libraries are so large that it is easy to get bogged down in detail and lose sight of the important concepts. We'll try to focus on the key byte and character stream classes and on the key methods of each class.



Technical Java. Applications for Science and Engineering
Technical Java: Applications for Science and Engineering
ISBN: 0131018159
EAN: 2147483647
Year: 2003
Pages: 281
Authors: Grant Palmer

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