Previous


1.1 Changes: Data I/O Streams

The DataIOTest example uses the DataInputStream.readLine method which is deprecated in JDK 1.1. The DataInputStream.readLine method does not properly convert bytes to characters. In the JDK 1.1, the preferred method for reading lines is BufferedReader.readLine.

Programs that use only the readLine method from DataInputStream can simply use a BufferedReader instead. To do this, replace code of the form:

DataInputStream d = new DataInputStream(in);
with:
BufferedReader d = new BufferedReader(new InputStreamReader(in));
However, other programs, such as the DataIOTest example that use other readXXX methods from DataInputStream must be rewritten because BufferedReader does not support these other methods. Such programs must be rewritten in a way that is particular to that program.

For example, the DataIOTest example can be modified to use DataInputStream's readChar method iteratively instead of readLine:

import java.io.*;

class DataIOTest {
    public static void main(String[] args) {

            // writing part
        try {
            DataOutputStream dos = new DataOutputStream(new FileOutputStream("invoice1.txt"));

            double[] prices = { 19.99, 9.99, 15.99, 3.99, 4.99 };
            int[] units = { 12, 8, 13, 29, 50 };
            String[] descs = { "Java T-shirt", "Java Mug", "Duke Juggling Dolls", "Java Pin", "Java Key Chain" };
        
            for (int i = 0; i < prices.length; i ++) {
                dos.writeDouble(prices[i]);
                dos.writeChar('\t');
                dos.writeInt(units[i]);
                dos.writeChar('\t');
                dos.writeChars(descs[i]);
                dos.writeChar('\n');
            }
            dos.close();
        } catch (IOException e) {
            System.err.println("DataIOTest: " + e);
        }

            // reading part
        try {
            DataInputStream dis = new DataInputStream(new FileInputStream("invoice1.txt"));
	    PrintWriter stdout = new PrintWriter(System.out, true);

            double price;
            int unit;
            double total = 0.0;
	    char chr;

            try {
                while (true) {
                    price = dis.readDouble();
                    dis.readChar();       // throws out the tab
                    unit = dis.readInt();
		    StringBuffer desc = new StringBuffer(20);
		    while ((chr = dis.readChar()) != '\n')
			    desc.append(chr);
                    stdout.println("You've ordered " + unit + " units of " + desc + " at $" + price);
                    total = total + unit * price;
                }
            } catch (EOFException e) {
            }
            stdout.println("For a TOTAL of: $" + total);
            dis.close();
        } catch (FileNotFoundException e) {
            System.err.println("DataIOTest: " + e);
        } catch (IOException e) {
            System.err.println("DataIOTest: " + e);
        }
    }
}
Note that DataIOTest has also been updated to use a PrintWriter to produce its output instead of System.out.


Previous