The Artima Developer Community
Sponsored Link

Java Buzz Forum
DataInputStream and DataOutPutStream

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
instanceof java

Posts: 576
Nickname: instanceof
Registered: Jan, 2015

instanceof java is a java related one.
DataInputStream and DataOutPutStream Posted: Mar 1, 2015 7:42 AM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by instanceof java.
Original Post: DataInputStream and DataOutPutStream
Feed Title: Instance Of Java
Feed URL: http://feeds.feedburner.com/blogspot/TXghwE
Feed Description: Instance of Java. A place where you can learn java in simple way each and every topic covered with many points and sample programs.
Latest Java Buzz Posts
Latest Java Buzz Posts by instanceof java
Latest Posts From Instance Of Java

Advertisement
  • 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.


Read: DataInputStream and DataOutPutStream

Topic: Do Primitives Need To Go? Previous Topic   Next Topic Topic: Advanced Java EE 7 Tricks

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use