In java, basic input-output stream provides a method for reading & writing bytes or character in a file.
If we want to read-write primitive data typa (such as int , float etc.) than we can use filter classes to filter data in the original stream.
DataInputStream & DataOutputStream are two filter classes used for creating “data streams” for handling primitive data type.
1 DataInputStream Class
In java DataInputStream class is used to allow application to read primitive data from different sources.
DataInputStream is a sub type of FilterInputStream.
A. DataInputStream Constructor
Sr No | Constructor |
---|---|
1 | DataInputStream(InputStream in) Take InputStream object to create DataInputStream |
Constructor call of DataInputStream
FileInputStream i = new FileInputStream("A.txt"); //open file A for read
DataInputStream I = new DataInputStream(i);
B Methods in DataInputStream
Following are the methods of DataInputStream
Sr No | Method |
---|---|
1 | public final int read(byte[] b) throws IOException Reads number of bytes from input stream and store in buffered array b |
2 | public final int read(byte[] b, int off, int len) throws IOException Reads up to len bytes from input stream and store in buffered array b |
3 | public final boolean readBoolean() throws IOException Reads one input byte and return true if byte is non zero, else false |
4 | public final byte readByte() throws IOException Reads and return one input byte |
5 | public final char readChar() throws IOException Reads two input byte and return a char |
6 | public final double readDouble() throws IOException Reads eight input byte and return a double value |
7 | public final float readFloat() throws IOException Reads four input bytes and return a float value |
8 | public final void readFully(byte[] b) throws IOException Reads some bytes from input stream and store in buffer array b |
9 | public final int readInt() throws IOException Reads four input bytes and return a integer value |
10 | public final long readLong() throws IOException Reads eight input bytes and returns a long value |
11 | public final short readShort() throws IOException Reads one input bytes and returns a short value |
12 | public final int readUnsignedByte() throws IOException Reads two byte and return unsigned byte value |
13 | public final String readUTF() throws IOException Reads a string that has been encoded in modified utf-8 format |
15 | public static final String readUTF(DataInput in) throws IOException Reads from a stream that has been encoded in modified utf-8 format |
16 | public final int skipBytes(int n) throws IOException Makes attempt to skip n bytes of data from input stream |
Example: Write a program to write & read a primitive data on a same file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | import java.io.*; public class PrimitiveReadWrite { public static void main(String args[]) throws IOException { FileOutputStream o = new FileOutputStream("A.txt"); //Open file “A” for write DataOutputStream O = new DataOutputStream(o); //write primitive data on file A O.writeInt(1999); O.writeDouble(333.89); O.writeBoolean(false); O.writeChar('X'); O.close(); o.close(); //read primitive data from file A FileInputStream i = new FileInputStream("A.txt"); //open file A for read DataInputStream I = new DataInputStream(i); System.out.println(I.readInt()); System.out.println(I.readDouble()); System.out.println(I.readBoolean()); System.out.println(I.readChar()); I.close(); i.close(); } } |
1 2 3 4 5 | Output: 1999 333.89 false X |