Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

The Data Streams DataInputStream

Ramesh Fadatare edited this page Aug 14, 2018 · 3 revisions

Java DataInputStream class allows an application to read primitive data from the input stream in a machine-independent way. Java application generally uses the data output stream to write data that can later be read by a data input stream.

DataInputStream Constructor

  • DataInputStream(InputStream in) - Creates a DataInputStream that uses the specified underlying InputStream.

DataInputStream Methods

  • int read(byte[] b) - Reads some number of bytes from the contained input stream and stores them into the buffer array b.
  • int read(byte[] b, int off, int len) - Reads up to len bytes of data from the contained input stream into an array of bytes.
  • boolean readBoolean() - See the general contract of the readBoolean method of DataInput.
  • byte readByte() - See the general contract of the readByte method of DataInput.
  • char readChar() - See the general contract of the readChar method of DataInput.
  • double readDouble() - See the general contract of the readDouble method of DataInput.
  • float readFloat() - See the general contract of the readFloat method of DataInput.
  • void readFully(byte[] b) - See the general contract of the readFully method of DataInput.
  • void readFully(byte[] b, int off, int len) - See the general contract of the readFully method of DataInput.
  • int readInt() - See the general contract of the readInt method of DataInput.
  • long readLong() - See the general contract of the readLong method of DataInput.
  • short readShort() - See the general contract of the readShort method of DataInput.
  • int readUnsignedByte() - See the general contract of the readUnsignedByte method of DataInput.
  • int readUnsignedShort() - See the general contract of the readUnsignedShort method of DataInput.
  • String readUTF() - See the general contract of the readUTF method of DataInput.
  • static String readUTF(DataInput in) - Reads from the stream in a representation of a Unicode character string encoded in modified UTF-8 format; this string of characters is then returned as a String.
  • int skipBytes(int n) - See the general contract of the skipBytes method of DataInput.

DataInputStream Example

A program to read each primitive char type from file sample.txt.

import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
public class DataInputStreamExample {
	public static void main(String[] args) {
		try(InputStream input = new FileInputStream("sample.txt"); 
	 DataInputStream inst = new DataInputStream(input);){ 
	 int count = input.available(); 
	 
	 byte[] ary = new byte[count]; 
	 inst.read(ary); 
	 for (byte bt : ary) { 
	 char k = (char) bt; 
	 System.out.print(k+"-"); 
	 } 
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

Output:

T-h-e-r-e- -i-s- -s-o-m-e- -c-o-n-t-e-n-t- -i-n- -f-i-l-e- -
-n-e-w-l-i-n-e- -a-d-d-e-d- -t-o- -e-x-i-s-t-i-n-g- -f-i-l-e-
-

Reference

DataInputStream

Clone this wiki locally

AltStyle によって変換されたページ (->オリジナル) /