Data Input and Output
An Introduction with Examples in Java |
Prof. David Bernstein |
Computer Science Department |
bernstdh@jmu.edu |
boolean readBoolean()
byte readByte()
char readChar()
double readDouble()
float readFloat()
int readInt()
long readLong()
short readShort()
writeBoolean(boolean v)
writeByte(int v)
writeChar(int v)
writeDouble(double v)
writeFloat(float v)
writeInt(int v)
writeLong(long v)
writeShort(int v)
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(); } }
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); } }
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
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
or
00000000 00000001
To "extract" the LSB:
01100001 00011010 & 00000000 11111111 _________________ 00000000 00011010
That is, perform a bitwise AND with the decimal number 255 (or 0x00FF).
To "extract" the MSB:
01100001 00011010 & 11111111 00000000 _________________ 01100001 00000000
That is, perform a bitwise AND with the decimal number 65280 (or 0xFF00).
Shifting Bits:
00000011 00000000 >> 8 _________________ 00000000 00000011
That is, 768 >> 8 evaluates to 3.
Combining Bytes:
00000011 00000000 | 00000000 00000001 _________________ 00000011 00000001
That is, 768 | 1 evaluates to 769.
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
?