• These classes are used to read and write data as primitive type from the underlying InputStreams and Output Streams.
  • DataInputStream and DataOutputStream classes have special methods to perform reading and writing operations as primitive data.

DataInputStream:

  • DataInputStream is a sub class of FilterInputStream and DataInput Interface.
  • It implements  below methods from the DataInput interface for reading bytes from a binary stream and convert them in to corresponding java primitive type.

 DataInputStream methods:

  • public byte readByte()
  • public short readShort()
  • public int readInt()
  • public long readLong()
  • public float readFloat()
  • public double readDouble()
  • public char readChar()
  • public boolean readBoolean()
  • public String readUTF()
  • public String readLine()

DataOutPutStream:

  • DataOutputStream class is a subclass of FilterOutputStream and DataOutput interface.
  • It implements below methods from DataOutput interface for converting data of any java primitive types to a stream of bytes and writing these bytes to a binary stream.


DataOutPutStream Methods:

  • public void writeByte(byte b)
  • public void writeShort(short s)
  • public void writeInt(int i)
  • public void writeLong(long l)
  • public void writeFloat(float f)
  • public void writeDouble(double d)
  • public void writeChar(char ch)
  • public void writeBoolean(boolean b)
  • public void writeUTF(String s)
  • public void writeBytes(String s)

  • To read data as primitive types using above readXxx() methods , the data must be written to the file as primitive types using writeXxx() methods.
  • readXxx() methods must be called in the same order in which the writeXxx() methods are called otherwise wrong value will be returned or EOFException will occur.

Program to Write data as primitive type using DataInputStream


  1. package instanceofjava;
  2.  
  3. import java.io.DataOutputStream;
  4. import java.io.FileOutputStream;
  5.  
  6. public class DOSDemo {
  7.  
  8. public static void main(String [] args) throws Exception{
  9.  
  10.   FileOutputStream fos= new FileOutputStream("data.txt");
  11.   DataOutputStream dos = new DataOutputStream(fos);
  12.  
  13.     dos.writeInt(37);
  14.     dos.writeFloat(2.15f);
  15.     dos.writeChar('c');
  16.     dos.writeBoolean(true);
  17.     dos.writeUTF("instance of java");
  18.  
  19.   }
  20. }



  • After compilation and execution in current working directory you can find the "data.txt".
  • Check data.txt file size.
  • To check file size "right click on the file"-> click on properties
  • You will get size = size of all primitive data types mentioned in program check it.
  • From this program we can prove boolean takes 1 byte, the minimum memory location size in java is 1 byte. 
  • For string data including double quotes JVM provides 1 byte for each character.

Program to Read data as primitive type using DataOutputStream

  1. package instanceofjava;
  2.  
  3. import java.io.DataInputStream;
  4. import java.io.FileInputStream;
  5.  
  6. public class DISDemo {
  7.  
  8. public static void main(String [] args) throws Exception{
  9.  
  10.     FileInputStream fis= new FileInputStream("D:\\data.txt");
  11.    DataInputStream dis = new DataInputStream(fis);
  12.  
  13.     int i=    dis.readInt();
  14.    float f=dis.readFloat();
  15.    char ch=dis.readChar();
  16.    boolean b=dis.readBoolean();
  17.    String str= dis.readUTF();
  18.  
  19.     System.out.println(i);
  20.     System.out.println(f);
  21.     System.out.println(ch);
  22.     System.out.println(b);
  23.     System.out.println(str);
  24.  
  25.     }
  26. }

  • Execute the above application by changing readXxx() methods calling order.
  • Then check you will get right value or not.
  • Actually readXxx() method read number of bytes from the file based on the data type.
  • For example if we call readInt() method it read 4 bytes from file.
  • So if our bytes are available in file program executed fine, execution terminates with EOFException.

Limitations in DataInputStream and DataOutputStream:

  • Using DataInputStream and DataOutputstream we can not read and write objects from persistence media.
  • They have only capability to read data up to only primitive data types.

Solution:

  • To read and write objects we must use ObjectInputStream and ObjectOutputStream.

Instance Of Java

We will help you in learning.Please leave your comments and suggestions in comment section. if you any doubts please use search box provided right side. Search there for answers thank you.
«
Next
Newer Post
»
Previous
Older Post

No comments

Leave a Reply

Select Menu