The following
classes here can use DataInput 's methods: DataInputStream class, ObjectInputStream
class, and RandomAccessFile
class
■ DataInput is the interface which defines basic methods for all of the byte input stream classes listed above. This section describes those methods.
■ DataInput’s methods can all throw IOExceptions. They are:
.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, and returns it in the range 0 to 255.
.readUnsignedShort( ) Reads two bytes and returns an
int in the range 0 to 65535.
.readUTF( ) Reads in a String that has been encoded using a modified UTF-8 format.
.skipBytes( num ) Skips num bytes of the input stream.
■ Most of these methods require you to catch their EOFException to properly recognize end of file. Not all the snippets below do that.
boolean
.readBoolean( ) method
■ Reads one input byte and returns true if that byte is nonzero, false if that byte is zero. i.e. This snippet writes two 1’s and then two zeroes, and reads them back, printing them as true true false false.
import
java.io.*;
try
{
File file = new
File("C:\\test.txt");
DataOutputStream dos = new DataOutputStream(
new FileOutputStream( file));
dos.writeBoolean(true); // write a 1
dos.write(1); // write a 1
dos.writeBoolean(false); // write a 0
dos.write(0); // write a 0
System.out.println(dos.size( ) + "
bytes were written");
dos.close( );
FileInputStream fis = new
FileInputStream(file);
DataInputStream dis = new
DataInputStream(fis);
for ( int x = 1; x <= dos.size( ); x++ )
{
System.out.println(dis.readBoolean(
));
}
dis.close( );
fis.close( );
file.delete( );
} catch (IOException e) { }
■ Reading text files. Reads and returns one input byte. i.e. This reads any text file, printing it as chars.
import
java.io.*;
try
{
DataInputStream dis = new DataInputStream(
new BufferedInputStream( new FileInputStream( "test.txt")));
while(true) System.out.print(
(char) dis.readByte( ) );
}
catch
(EOFException eof) { System.out.println(eof);
}
catch (IOException io) { }
■ Reads an input char and returns the char value. Note this is not a single-byte-oriented method. It assumes two bytes. Works best with the .writeChar( int ) method of DataOutput. See DataOutput and .writeChar( int ) for a complete example.
■ Reads eight input bytes and returns a double value. i.e. This snippet writes 1.0, 2.0, and 3.0 out as doubles and then reads them back and prints them.
import
java.io.*;
try
{
FileOutputStream fos = new
FileOutputStream( "my_double_output" );
DataOutputStream dos = new
DataOutputStream( fos );
for ( double d = 1; d < 4.0; d++ ) {
dos.writeDouble( d );
}
dos.close ( );
DataInputStream dis = new DataInputStream(
new BufferedInputStream( new FileInputStream( "my_double_output")));
for ( int x = 0; x < 3.0; x++ ) {
System.out.println(String.valueOf(dis.readDouble( ) ));
}
} catch (IOException e) { }
■ Reads four input bytes and returns a float value. See example for readDouble( ) just above.
■ Reads some bytes from an input stream and stores them into a buffer array. i.e. The snippet writes, and then reads back, stores, and prints the hex bytes representing A, B, C.
import
java.io.*;
import
java.util.*;
try
{
FileOutputStream fos = new
FileOutputStream( "my_byte_output" );
DataOutputStream dos = new
DataOutputStream( fos );
byte[ ] b = new byte[ ] { (byte) 0x61,
(byte) 0x62, (byte) 0x63 };
dos.write( b );
dos.close( );
byte[ ] ba = new byte[ 3 ];
DataInputStream dis = new DataInputStream(
new BufferedInputStream( new FileInputStream( "my_byte_output")));
for ( int x = 0; x < 3.0; x++ ) {
dis.readFully(ba);
for ( x = 0; x < ba.length; x++ ) System.out.println( (char) ba[ x ] );
}
} catch (IOException e) { }
void .readFully( byte[ ], offset, len )
■ Just like above, except it reads back len bytes, starting at offset in the byte array, whereas readFully( ) reads back bytes up to the length of the array. See above example. Also see a similar example under InputStream class.
■ Reads four input bytes and returns an int value. See DataOutput’s corresponding .writeInt(int) method. i.e. This snippet write some ints then reads them back in and prints their values.
import
java.io.*;
try
{
FileOutputStream fos = new
FileOutputStream( "my_int_output_filename" );
DataOutputStream dos = new
DataOutputStream( fos );
for ( int x = 0; x < 5; x++ ) {
dos.writeInt( x );
}
dos.close ( );
DataInputStream dis = new DataInputStream(
new BufferedInputStream( new FileInputStream( "my_int_output_filename")));
for ( int x = 0; x < 5; x++ ) {
System.out.print(
String.valueOf(dis.readInt( ) ));
}
} catch (IOException e) { }
■ Reads the next line of text from the input stream and returns it as a String. Reads a line up to a \r carriage return, a \n line feed, or to a \r\n combination, and then stops. Note that these characters are not returned.
■ This is the same as the readLine( ) method of BufferedReader. In fact, DataInputStream’s version of readLine( ) has been deprecated, and you have to use the one from the char side. See BufferedReader’s readLine( )
■ Reads eight input bytes and returns a long value. You catch its EOF exception to recognize end-of-file. i.e.
import
java.io.*;
try
{
FileOutputStream fos = new
FileOutputStream( "my_long_output" );
DataOutputStream dos = new DataOutputStream(
fos );
for ( long l = 10; l <= 15; l++ ) {
dos.writeLong( l );
}
dos.close ( );
DataInputStream dis = new DataInputStream(
new BufferedInputStream( new FileInputStream( "my_long_output")));
while (true) {
System.out.println(String.valueOf(dis.readLong( ) ));
}
}
catch
(EOFException eof) {System.out.println(eof);
}
catch (IOException io) { }
■ Reads two input bytes and returns a short value. Same example as above, except with shorts this time.
int .readUnsignedByte( ) method
■ Reads one input byte, zero-extends it to type int, and returns the result, which is therefore in the range 0 through 255.
int .readUnsignedShort( ) method
■ Reads two input bytes and returns an int value in the range 0 through 65535.
■ Reads in a string that has been encoded using a modified UTF-8 format.
■ Skips num bytes of the input stream. The snippet reads a file containing Mary had a little lamb but then skips its first 11 bytes when reading it back in, printing only little lamb i.e.
import
java.io.*;
try
{
DataInputStream dis = new DataInputStream(
new BufferedInputStream( new FileInputStream( "test.txt")));
dis.skipBytes(11);
while(true) System.out.print(
(char) dis.readByte( ) );
}
catch
(EOFException eof) {System.out.println(eof);
}
catch (IOException io) { }