Advertisement
DataInputStream(InputStream is) |
---|
This constructor takes an InputStream object in its argument to read data out of this input stream.
Example-:
|
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
//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);
}
}
}
Number of bytes available to read = 27
1985
245.45
c
true
9999999999999
12.15
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement