DataInputStream is the complement of DataOuputStream. It extends FilterInputStream,
which extends InputStream, and it implements the DataInput interface. Here is its only
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:
class DataIODemo {
public static void main(String args[])
throws IOException {
FileOutputStream fout = new FileOutputStream("Test.dat");
DataOutputStream out = new DataOutputStream(fout);
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);
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