|
SYS-CON.TV Webcasts
Comments
Did you read today's front page stories & breaking news?
SYS-CON.TV
|
Top Links You Must Click On
General Java Programming with Java's I/O Streams - Part 1
Programming with Java's I/O Streams - Part 1
By: Anil Hemrajani
Dec. 1, 1998 12:00 AM
The APIs Java provides for reading and writing streams of data have been part of the core Java Development Kit since version 1.0, but they're often overshadowed by the better known JavaBeans, JFC, RMI, JDBC and others. However, input and output streams are the backbone of the Java APIs, and understanding them is not only crucial but can also make programming with them a lot of fun. In this article we'll cover the fundamentals of I/O streams by looking at the various stream classes and covering the concept of stream chaining. Next month we'll look at some example uses of I/O streams.
Overview
Character versus Byte Streams Most of the functionality available for byte streams is also provided for character streams. The methods for character streams generally accept parameters of data type "char" parameters, while "byte" streams - you guessed it - work with "byte" data types. The names of the methods in both sets of classes are almost identical except for the suffix; that is, character-stream classes end with the suffix Reader or Writer and byte-stream classes end with the suffix InputStream and OutputStream. For example, to read files using character streams, you'd use the java.io.FileReader class; to read using byte streams you'd use java.io.FileInputStream.
Unless you're working with binary data such as image and sound files, you should use readers and writers to read and write information for the following three reasons: To bridge the gap between the byte and character-stream classes, Java provides the java.io.InputStreamReader and java.io.OutputStreamWriter classes. The only purpose of these classes is to convert byte data into character-based data according to a specified (or the platform default) encoding. For example, the static data member "in" in the "System" class is essentially a handle to the Standard Input (stdin) device. If you wanted to "wrap" this inside the java.io.BufferedReader class that works with character streams, you'd use InputStreamReader class as follows:
BufferedReader in = new BufferedReader(new
For JDK 1.0 Versions
The Various Stream Classes
int read() Listing 1 demonstrates how the read and write methods can be used. The program is similar to the MS-DOS type and Unix cat commands, that is, it displays the contents of a file. The following code fragment from Listing 1 opens the input and output streams:
FileReader fr = new FileReader(args[0]); The program then reads the input file and displays its contents till it hits an end of file condition (-1), as shown here:
while ((read = fr.read(c)) != -1) I used the "(char cbuf[])" version of the read method, because reading a single character at a time can be approximately five times slower than reading chunks (array) at a time.
Other notable methods in the top level classes include skip(int), mark(int), reset(), available(), ready() and flush().
Specialized Descendant Stream Classes The class hierarchy shown in Listing 4 portrays a few of the specialized classes found in the java.io package. This hierarchy merely demonstrates how stream classes extend their parent classes (e.g.. LineNumberReader) to add more specialized functionality. Tables 1, 2 and 3 provide a more comprehensive list of the various descendant classes found in the java.io and other packages, along with a brief description for each class. These descendant classes are divided into two categories: those that read from or write to "data sinks", and those that perform some sort of processing on the data (this distinction is merely to group the classes into two logical sections; you don't have to know one way or the other when using them). Listings 2 and 3 don't contain the complete list for the table because I intentionally skipped the "byte" counterparts to the "char" based classes and a few others (please refer to the JDK API reference guide for a complete list).
Stream Chaining Stream chaining is the concept of "connecting" several stream classes together to get the data in the form required. Each class performs a specific task on the data and forwards it to the next class in the chain. Stream chaining can be very handy. For example, in our own 100% Pure Java backup software, BackOnline, we chain several stream classes to compress, encrypt, transmit, receive and finally store the data in a remote file. Figure 1 portrays chaining of three classes to convert raw data into compressed and encrypted data, which is stored in a local file. The data is written to GZIPOutputStream, which compresses the input data and sends it to CryptOutputStream. CryptOutputStream encrypts the data prior to forwarding it to FileOutputStream, which writes it out to a file. The result is a file that contains encrypted and compressed data. The source for the stream chaining shown in Figure 1 would look something like the code seen here:
FileOutputStream fos = new FileOutputStream("myfile.out"); or simply:
GZIPOutputStream gos = new To write to chained streams, simply call the write() method on the outermost class as shown here: gos.write('a'); Similarily, when closing chained streams, you need only to close the outermost stream class since the close() call is automatically trickled through all the chained classes; in our example above we would simply call the close() method on the GZIPOutputStream class.
Summary Reader Feedback: Page 1 of 1
Enterprise Open Source Magazine Latest Stories . . .
Subscribe to the World's Most Powerful Newsletters
Subscribe to Our Rss Feeds & Get Your SYS-CON News Live!
|
SYS-CON Featured Whitepapers
Most Read This Week |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||