Java.io - SPLessons

Java.io DataInputStream

Home > Lesson > Chapter 11
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Java.io DataInputStream

Java.io DataInputStream

shape Introduction

The Java.io DataInputStream is the class used for to read data from file or into the file. The main feature of this Java.io DataInputStream is to read data greater than bytes like int, float, double.

shape Description

Java.io DataInputStream - Generally the input stream will take care of reading bytes. But instead of reading bytes the data input stream can read primitives in java like int, long, float so the input stream is wrap with data input stream. Here the close() method is not called explicitly the try with resources take care of closing the data input stream. The data which was written by data output stream is reads by data input stream.

shape Conceptual figure

Class Declaration

shape Declaration

Java.io/DataInputStream class is declared below: Public abstract class DataInputStream extends FilterInputStream  implements DataInput

shape Fields

Java.io/DataInputStream class fields are:
  • Protected InputStream in :The field represents the current stream have to filter.

Class Constructors

shape Table

Constructor Description
DataInputStream(InputStream in) The function of this constructor is to create the data input stream instance and used to read from input stream.

Class Methods

shape Table

Method Description
int read  (byte[] b) By using this method it reads the data up to b.length bytes from the input stream.
void read fully(byte[] b) The function of this method is to read the bytes from the input stream and store in array buffer.
int skipBytes(int n) From the input stream this method skips over and throw out ‘n’ bytes of data.
int read(byte[] b, int off, int n) By using this method it reads the data up to 'n' bytes from the input stream to an array  of the bytes.
String readUTF() The function of this method is to read the content in the string format and have encoded by UTF-8 format.
int readInt() The function of this method is to read 4- bytes from input stream.

Inherited Methods

shape Description

Java.io DataInputStream - From the following classes , methods are inherited to data input stream class.
  • Java.io.Object
  • Java.io.FilterInputStream

shape Examples

Usage of int read (byte[] b) method. [c]import java.io.DataInputStream; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; public class DataInputStream { public static void main(String[] args) throws IOException { InputStream is = null; DataInputStream dis = null; try{ // creating input stream from file input stream is = new FileInputStream("D:\\test.txt"); // creating data input stream dis = new DataInputStream(is); // count the available bytes form the input stream int count = is.available(); // creating buffer byte[] bs = new byte[count]; // To read data into buffer dis.read(bs); // For to represent each byte in the buffer for (byte b:bs) { // To convert byte into character char c = (char)b; // To print the character System.out.print(c+" "); } }catch(Exception e) { // if any I/O error occurs e.printStackTrace(); }finally { // Releases all the associated system files with this stream if(is!=null) is.close(); if(dis!=null) dis.close(); } } }[/c] Output The result is as follows. [c]S P L E S S O N S [/c] Usage of void read fully(byte[] b) method. [c]import java.io.DataInputStream; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; public class DataInputStream { public static void main(String[] args) throws IOException { InputStream is = null; DataInputStream dis = null; try{ // creating a new file input stream is = new FileInputStream("D:\\test.txt"); // creating a new data input stream dis = new DataInputStream(is); // the available stream have to read int length = dis.available(); // creating buffer byte[] buf = new byte[length]; // To read the full data into the buffer dis.readFully(buf); // for to represent each byte in the buffer for (byte b:buf) { // To convert byte to char char c = (char)b; // To print character System.out.print(c); } }catch(Exception e) { // if any error occurs e.printStackTrace(); }finally { // releases all the system resources from this streams if(is!=null) is.close(); if(dis!=null) dis.close(); } } }[/c] Output The result is as follows. [c]SPLESSONS@ItoolsInfo.com[/c]

Summary

shape Key Points

  • Java.io DataInputStream - The data input stream is the class used for to read data from file or into the file.
  • The main feature of this data input stream is to read data greater than bytes like int, float, double.