■ For its methods, see DataInput interface and InputStream class
|
It has these methods because it implements DataInput: |
Plus it has these methods because it extends InputStream: |
|
.readBoolean( ) Reads one byte and returns
true or false if it was zero
or non-zero. .readByte( ) Reads and returns a byte. .readChar( ) Reads and returns a char. .readDouble( ) Reads eight bytes and
returns a double. .readFloat( ) Reads four bytes and
returns a float. .readFully( byte[ ]
) Reads bytes into an array. .readFully( byte[ ],
offset, len) Reads len bytes into an
arry starting at offset. .readInt( ) Reads four bytes and
returns an int. .readLine( ) Reads the next line of
text from the input stream. .readLong( ) Reads eight bytes and
returns a long. .readShort( ) Reads two bytes and
returns a short. .readUnsignedByte(
) Reads a byte, extends it
to int, returns it in the range 0 -255. .readUnsignedShort(
) Reads two bytes and
returns an int in range 0 to 65535. .readUTF( ) Reads in a String encoded
using a modified UTF-8 format. .skipBytes( num ) Skips num bytes of the input stream. |
available( ) Tells how many bytes can be gotten without blocking. close( ) Close the stream. mark( int
readAheadLimit ) Mark the present position in
the stream. markSupported( ) Tell whether this stream supports mark(...). read( ) Read a
single byte, returning it as the low end of an int. read( byte[ ] b ) Read bytes into a byte array. read( byte[ ] b, int
off, int len) Read bytes into a portion of
a byte array. ready( ) Tell whether this stream is ready to be read. reset( ) Reset
the stream to the beginning. skip( long n ) Skip characters. |
■ ObjectInputStream reads back:
(1) Serializable or Externalizable objects written with OutputStream.
(2) Bytes and primitives written with ObjectOutputStream
■ All object inputs, including Strings and Arrays, have to be cast upon receipt.
■ Restoral includes loading and initializations.
It adds one notable method which reads an object from the ObjectInputStream:
■ This sample code writes and then reads an int then two objects in order:
import java.io.*;
import java.util.*;
try {
FileOutputStream fos
= new FileOutputStream("obj.tmp");
ObjectOutputStream
oos = new ObjectOutputStream(fos);
int i = 123;
String s = "a
String";
Date d = new Date(
);
oos.writeInt( i ); //
write it
oos.writeObject( s
);
oos.writeObject( d
);
oos.close( );
FileInputStream fis
= new FileInputStream("obj.tmp");
ObjectInputStream
ois = new ObjectInputStream(fis);
int i2 =
ois.readInt( ); //
read it
String s2 =
(String) ois.readObject( );
Date d2 = (Date)
ois.readObject( );
fis.close( );
}
catch (Exception e) { }