JMU
Data Input and Output
An Introduction with Examples in Java


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu


Methods in the DataInputStream java.io.DataInputStream Class
Methods in the DataOutputStream java.io.DataOutputStream Class
Things are Not as Simple as they Appear
Complications When Using the Console

The Following Seems Fine

javaexamples/streams/DataWriter.java
        import java.io.*;

/**
 * An example of value-type I/O 
 * (that might not behave as expected)
 *
 * @author  Prof. David Bernstein, James Madison University
 * @version 1.0
 */
public class DataWriter
{

    /**
     * The entry point
     *
     * @param args   The command-line arguments
     */
    public static void main(String[] args) throws Exception
    {
       DataOutputStream    out;
       double              d;
       int                 i;


       i = 97;
       d = 97.0;

       out = new DataOutputStream(System.out);
       out.writeInt(i);
       out.writeChar('|');

       out.writeDouble(d);
       out.writeChar('|');

       out.flush();
    }

}
        
Complications When Using the Console (cont.)
Complications When Using the Console (cont.)

The Following Also Seems Fine

javaexamples/streams/DataReader.java
        import java.io.*;

/**
 * An example of value-type I/O 
 * (that might not behave as expected)
 *
 * @author  Prof. David Bernstein, James Madison University
 * @version 1.0
 */
public class DataReader
{

    /**
     * The entry point
     *
     * @param args   The command-line arguments
     */
    public static void main(String[] args) throws Exception
    {
       DataInputStream     in;
       double              d;
       int                 i;
       
       
       in = new DataInputStream(System.in);
       System.out.print("\nEnter an int: ");
       i = in.readInt();
       System.out.println("\nYou entered "+i);
       
       System.out.print("\nEnter a double: ");
       d = in.readDouble();
       System.out.println("\nYou entered "+d);
    }

}
        
Complications When Using the Console (cont.)
Byte Order

Using one byte to represent unsigned integers is straightforward. For example, using the usual conventions, the decimal number 26 can be represented as follows:

  00011010
  

Byte Order (cont.)

Using two bytes to represent unsigned integers is a little trickier. For example, which of the following is the correct representation of the number 256?

  00000001 00000000
  
Most significant byte first (Big-endian or network byte order)

or

  00000000 00000001
  
Least significant byte first (Little-endian)

Byte Order (cont.)
Re-Ordering Bytes
Re-Odering Bytes (cont.)

To "extract" the LSB:

        01100001 00011010
  &     00000000 11111111
        _________________
        00000000 00011010
  

That is, perform a bitwise AND with the decimal number 255 (or 0x00FF).

Re-Odering Bytes (cont.)

To "extract" the MSB:

        01100001 00011010
  &     11111111 00000000 
        _________________
        01100001 00000000
  

That is, perform a bitwise AND with the decimal number 65280 (or 0xFF00).

Re-Odering Bytes (cont.)

Shifting Bits:

        00000011 00000000 
   >>   8
        _________________
        00000000 00000011
  

That is, 768 >> 8 evaluates to 3.

Re-Odering Bytes (cont.)

Combining Bytes:

        00000011 00000000 
      | 00000000 00000001
        _________________
        00000011 00000001
  

That is, 768 | 1 evaluates to 769.

Byte Order (cont.)
Returning to an Earlier Example
javaexamples/streams/DataWriter.java
        import java.io.*;

/**
 * An example of value-type I/O 
 * (that might not behave as expected)
 *
 * @author  Prof. David Bernstein, James Madison University
 * @version 1.0
 */
public class DataWriter
{

    /**
     * The entry point
     *
     * @param args   The command-line arguments
     */
    public static void main(String[] args) throws Exception
    {
       DataOutputStream    out;
       double              d;
       int                 i;


       i = 97;
       d = 97.0;

       out = new DataOutputStream(System.out);
       out.writeInt(i);
       out.writeChar('|');

       out.writeDouble(d);
       out.writeChar('|');

       out.flush();
    }

}
        

What will be printed if we assign 97 + (65 << 8) to i?