Advertisement



< Prev
Next >



DataInputStream



InputStream classes reads data in terms of bytes but DataInputStream class is used to read data in terms of primitive data types such as short, char, int, float, double, boolean from an input stream. DataInputStream class is a filter class which is used to wrap any input stream to read primitive data types out of it.

Note : DataInputStream is a subclass of FilterInputStream class, which is a subclass of InputStream class.




Constructor of DataInputStream :


DataInputStream(InputStream is)
This constructor takes an InputStream object in its argument to read data out of this input stream.
Example-:
FileInputStream fis=new FileInputStream("D://TextBook1.txt");
DataInputStream dis =new DataInputStream(fis);
We have created a DataInputStream object to read primitivedata types out of a file D:\\TextBook1.txt, pointed by FileInputStream object, fis.




Point to remember


FileIntputStream is a subclass of InputStream




Methods for reading DataInputStream


Some methods that we generally use while working with BufferedInputStream are shown in the table below :

Methods Description
available()
This method gives the remaining number of bytes available to read from this input stream.

read()
This method reads one byte at a time from this input stream.

read(byte[])
This method reads a whole byte array at a time from this input stream.

readChar()
This method reads a character(primitive data type) at a time from this input stream.

readFloat()
This method reads a float (primitive data type) at a time from this input stream.

readShort()
This method reads a short(primitive data type) at a time from this input stream.

readInt()
This method reads an int(primitive data type) at a time from this input stream.

readLong()
This method reads a long(primitive data type) at a time from this input stream.

readBoolean()
This method reads a boolean(primitive data type) at a time from this input stream.

close()
This method closes this input stream and also frees any resources connected with this input stream.




Advertisement




Reading primitive data types out of an existing file using DataInputStream.


In this program, we will create a DataInputStream object to read primitive data types from a file D:\\TextBook.txt, pointed by a FileInputStream object reference. Let's say this file TextBook.txt exists in the D: drive, with contents -:

1985
245.45
c
true
9999999999999
12.15

//Program to read primitive data types out of a file using DataInputStream

import java.io.*;

class A
{
public static void main(String... ar)
{
try
{
FileInputStream fis=new FileInputStream("D://TextBook.txt");
DataInputStream dis =new DataInputStream(fis);

System.out.println("Number of bytes available to read = "+ fis.available());

System.out.println(dis.readInt());
System.out.println(dis.readDouble());
System.out.println(dis.readChar());
System.out.println(dis.readBoolean());
System.out.println(dis.readLong());
System.out.println(dis.readFloat());

fis.close();
dis.close();

}
catch(IOException e)
{
System.out.println(e);
}


}
}


Output


Number of bytes available to read = 27
1985
245.45
c
true
9999999999999
12.15


Program Analysis





Please share this article -




< Prev
Next >
< SequenceInputStream
DataOutputStream >



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement



Notifications



Please check our latest addition

C#, PYTHON and DJANGO


Advertisement