DataInputStream is the complement of DataOuputStream. It extends FilterInputStream,
which extends InputStream, and it implements the DataInput interface. Here is its only
constructor:
DataInputStream(InputStream inputStream)
Here, inputStream specifies the input stream from which data will be read.
Like DataOutputStream, DataInputStream supports all of the methods of its superclasses,
but it is the methods defined by the DataInput interface that make it unique. These methods
read a sequence of bytes and convert them into values of a primitive type. Here is a sampling
of these methods:
double readDouble( ) throws IOException
boolean readBoolean( ) throws IOException
int readInt( ) throws IOException
The following program demonstrates the use of DataOutputStream and DataInputStream:
import java.io.*;
class DataIODemo {
public static void main(String args[])
throws IOException {
FileOutputStream fout = new FileOutputStream("Test.dat");
DataOutputStream out = new DataOutputStream(fout);
out.writeDouble(98.6);
out.writeInt(1000);
out.writeBoolean(true);
out.close();
FileInputStream fin = new FileInputStream("Test.dat");
DataInputStream in = new DataInputStream(fin);
double d = in.readDouble();
int i = in.readInt();
boolean b = in.readBoolean();
System.out.println("Here are the values:  " +
d + " " + i + " " + b);
in.close();
}
}
The output is shown here:
Here are the values:
98.6 1000 true
Search WWH :
Custom Search
Previous Page
Java SE 6 Topic Index
Next Page
Java SE 6 Bookmarks
Home