Primitive Data
To read and write primitive data types, Java provides two
classes with appropriate methods.
java.io.DataInputStream
Useful for reading Java primitve types.
- extends FilterInputStream
- implements DataInput
- constructor
- public DataInputStream( InputStream in ) throws
IOException;
- Additional read methods:
- public boolean readBoolean() throws
IOException;
- public byte readByte() throws IOException;
- public char readChar() throws IOException;
- public double readDouble() throws
IOException;
- public float readFloat() throws IOException;
- public int readInt() throws IOException;
- public long readLong() throws IOException;
- public short readShort() throws IOException;
java.io.DataOutputStream
Useful for writing Java primitve types.
- extends FilterOutputStream
- implements DataOutput
- constructor
- public DataOutputStream( OutputStream out ) throws
IOException;
- Synchronizes an inherited method:
- public synchronized void write( int b ) throws
IOException;
- Additional write methods:
- public void writeBoolean( boolean b ) throws
IOException;
- public void writeByte( int b ) throws
IOException;
- public void writeBytes( String s ) throws
IOException;
- public void writeChar( int c ) throws
IOException;
- public void writeChars( String s ) throws
IOException;
- public void writeDouble( double d ) throws
IOException;
- public void writeFloat( float f ) throws
IOException;
- public void writeInt( int i ) throws
IOException;
- public void writeLong( long l ) throws
IOException;
- public void writeShort( int s ) throws
IOException;
Objects
To read and write Objects as well as primitive data
types, Java provides two additional classes that support all
of the methods of DataInput/OutputStream, plus additional
support for Object reading and writing.
Note that an Object to be written or read must implement
the Serializable interface. Note also that Object streams do
a lot of additional work in packaging/unpackaging Objects to
read or write them. If you are using a stream in a
time-critical way -- such as to send information between two
players of a fast-paced video game -- you may wish to send
primitive data, such as ints, rather than Objects
encapsulating that data, such as Points.
java.io.ObjectInputStream
Useful for reading Serializable Objects as well as Java
primitve types.
- extends InputStream
- implements ObjectInput
- constructor
- public ObjectInputStream( InputStream in ) throws
IOException, StreamCorruptedException;
- Object read method:
- public Object readObject();
- Plus the primitive data methods listed in
DataInputStream
java.io.ObjectOutputStream
Useful for writing Serializable Objects as well as Java
primitve types.
ava primitve types.
- extends OutputStream
- implements ObjectOutput
- constructor
- public ObjectOutputStream( OutputStream out )
throws IOException;
- Object write method:
- public void writeObject( Object o );
- Plus the primitive data methods listed in
DataOutputStream
|