Apr 8, 2012

An overview of the java.io package

This section introduces the java.io package. Here are some basic points about I/O:
Data in files on your system is called persistent data because it persists(continue to exist) after the program runs. Files are created through streams in Java code. A stream is a linear, sequential flow of bytes of input or output data. Streams are written to the file system to create files. Streams can also be transferred over the Internet.
Three streams are created for us automatically:
System.out - standard output stream
System.in - standard input stream
System.err -standard error

Basic input and output classes
           The java.io package contains a fairly large number of classes that deal with Java input and output. Most of the classes consist of:
                     ·           Byte streams that are subclasses of InputStream or OutputStream
                     ·         Character streams that are subclasses of Reader or Writer

The Reader and Writer classes read and write 16-bit Unicode characters. InputStream reads 8-bit bytes, while OutputStream writes 8-bit bytes. As their class name suggests, ObjectInputStream  and ObjectOutputStream transmit entire objects. ObjectInputStream reads objects; ObjectOutputStream writes objects.
Unicode is an international standard character encoding that is capable of representing most of the world's written languages. In Unicode, two bytes make a character.
Using the 16-bit Unicode character streams makes it easier to internationalize your code. As a result, the software is not dependent on one single encoding.

What to use
        There are a number of different questions to consider when dealing with the java.io package:
Ø  What is your format: text or binary?
Ø  Do you want random access capability?
Ø  Are you dealing with objects or non-objects?
Ø  What are your sources and sinks for data?
Ø  Do you need to use filtering?

Text or binary
      What's your format for storing or transmitting data? Will you be using text or binary data?
            *  If you use binary data, such as integers or doubles, then use the InputStream and OutputStream classes.
            *  If you are using text data, then the Reader and Writer classes are right.

Random access
        Do you want random access to records? Random access allows you to go anywhere within a file and be able to treat the file as if it were a collection of records.
The RandomAccessFile class permits random access. The data is stored in binary format. Using random access files improves performance and efficiency.

Object or non-object
         Are you inputting or outputting the attributes of an object? If the data itself is an object, then use the ObjectInputStream and ObjectOutputStream classes.

Sources and sinks for data
         What is the source of your data? What will be consuming your output data that is, acting as a sink? You can input or output your data in a number of ways: sockets, files, Strings, and arrays of characters.
Any of these can be a source for an InputStream or Reader or a sink for an OutputStream or Writer.

      Do you need filtering for your data? There are  couple of ways to filter data.
     ü  Buffering is one filtering method. Instead of going back to the operating system for each byte, you can use an object to provide a buffer.
     ü  Checksumming is another filtering method. As you are reading or writing a stream, you might want to compute a checksum on it. A checksum is a value you can use later on to make sure the stream was transmitted properly.

Storing data records
        A data record is a collection of more than one element, such as names or addresses. There are three ways to store data records:
Ø  Use delimited records, such as a mail-merge style record, to store values. On output, data values are converted to strings separated by delimiters such as the tab character, and ending with a new-line character. To read the data back into Java code, the entire line is read and then broken up using the StringTokenizer class.
Ø  Use fixed size records to store data records. Use the RandomAccessFile class to store one or more records. Use the seek() method to find a particular record. If you choose this option to store data records, you must ensure strings are set to a fixed maximum size.
Ø  Alternatively, you can use variable length records if you use an auxiliary file to store the lengths of each record. Use object streams to store data records. If object streams are used, no skipping around is permitted, and all objects are written to a file in a sequential manner.

Creating streams: Example code
           Here is an example of how to create a stream that reads and writes characters using a TCP/IP socket as the sink and source. The classes themselves are explained later.
First, we create a TCP/IP socket object that is connected to www.google.com and port 80. This is the Web server port. The method getInputStream() in the Socket class returns an InputStream ,which represents byte-by-byte reading of the socket. The InputStream is used to create an InputStreamReader , which transforms the bytes read from the socket into characters. A BufferedReader class is created, which reads from the InputStreamReader and buffers the characters into its own internal buffer. The object named in then reads characters from that buffer, but the ultimate source of the characters is the Web server at www.google.com.
On the other hand, the getOutputStream() method of the Socket class returns a reference to an OutputStream , which writes a byte at a time. The PrinterWriter constructor uses that OutputStream as the sink for the characters it writes out. When a character is written to the object named out , it is ultimately sent to the Web server at www.google.com.
This example treats the data stream as character data:

       Socket a_socket = new Socket(www.google.com, 80);
       InputStreamReader isr = new  InputStreamReader(a_socket.getInputStream());
       BufferedReader in = new BufferedReader (isr);
       PrinterWriter out = new  PrinterWriter(a_socket.getOutputStream());

A second way to use streams is to use them to transmit binary data. We create a TCP/IP socket object that is connected to www.google.com and port 100. We construct a DataInputStream using the InputStream returned by getInputStream() and a DataOutputStream using the OutputStream returned by getOutputStream() . We can send and receive integers or doubles or other binary data over these two streams.
This example treats the data stream as binary data:

       Socket socket_data = new Socket(www.google.com, 100);
       DataInputStream in_data = new  DataInputStream(socket_data.getInputStream());
       DataOutputStream out_data = new  DataOutputStream(socket_data.getOutputStream());

Almost every input or output method throws an exception. Therefore, any time you do I/O, you need to catch exceptions. There is a large hierarchy of I/O exceptions derived from IOException. Typically you can just catch IOException , which catches all the derived exceptions. However, some exceptions thrown by I/O methods are not in the IOException hierarchy. One such exception is the java.util.zip.DataFormatException.This exception is thrown when invalid or corrupt data is found while data being read from a zip file is being uncompressed. java.util.zip.DataFormatException has to be caught explicitly because it is not in the IOException hierarchy. Remember, exceptions in Java code are thrown when something unexpected happens.


Post a Comment