Java DataInputStream Class


In Java language, DataInputStream is used to read primitives data from the input stream.

 

In generally java application uses the data output stream to write data and it read the data input stream. It reads the data instead of just bytes.

Syntax:

public class DataInputStream extends FilterInputStream implements DataInput  

To create an InputStream

 InputStream in = new DataInputStream(InputStream in);

DataInputStream Class Methods

S.No Method Description
1 Public final int read(byte [] a) This method is used to read the number of bytes from the input stream.
2 public final int read(byte[] a, int off, int len) This method is used to read len bytes of data from the input stream.
3 public final boolean readBoolean() This method is used to read input byte and return true when byte is non zero otherwise return false.
4 public final byte readByte() This method is used to read and return the one input byte.
5 public String readLine() This method is used to reads some bytes from the inputstream an stores in to the byte array.
6 public final int readInt() This method is used to read input bytes and return an int value.

Example:

import java.io.*;
public class DataInputStreamDemo{
    public static void main(String args[]) throws IOException{  
		DataInputStream datain = new DataInputStream(new FileInputStream("F:\\out.txt"));
        int c= datain.available(); 
        byte[] arr = new byte[c];  
    	inst.read(arr);  
    	for (byte b : arr) {  
      		char i = (char) b;  
      		System.out.print(i+" ");  
    	}  
    }
} 

Output

 S u c c e s s

out.txt

Success 



Onlinetpoint is optimized for basic learning, practice and more. Examples are well checked and working examples available on this website but we can't give assurity for 100% correctness of all the content. This site under copyright content belongs to Onlinetpoint. You agree to have read and accepted our terms of use, cookie and privacy policy.