The DataIOTest example uses theDataInputStream.readLine
method which is deprecated in JDK 1.1. TheDataInputStream.readLine
method does not properly convert bytes to characters. In the JDK 1.1, the preferred method for reading lines isBufferedReader.readLine
.Programs that use only the
readLine
method fromDataInputStream
can simply use aBufferedReader
instead. To do this, replace code of the form:with:DataInputStream d = new DataInputStream(in);However, other programs, such as theBufferedReader d = new BufferedReader(new InputStreamReader(in));DataIOTest
example that use otherreadXXX
methods fromDataInputStream
must be rewritten becauseBufferedReader
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
'sreadChar
method iteratively instead ofreadLine
:Note that DataIOTest has also been updated to use aimport 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); } } }PrintWriter
to produce its output instead ofSystem.out
.