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.
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
package instanceofjava;
import java.io.DataOutputStream;
import java.io.FileOutputStream;
public class DOSDemo {
public static void main(String [] args) throws Exception{
FileOutputStream fos= new FileOutputStream("data.txt");
DataOutputStream dos = new DataOutputStream(fos);
dos.writeInt(37);
dos.writeFloat(2.15f);
dos.writeChar('c');
dos.writeBoolean(true);
dos.writeUTF("instance of java");
}
}
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
package instanceofjava;
import java.io.DataInputStream;
import java.io.FileInputStream;
public class DISDemo {
public static void main(String [] args) throws Exception{
FileInputStream fis= new FileInputStream("D:\\data.txt");
DataInputStream dis = new DataInputStream(fis);
int i= dis.readInt();
float f=dis.readFloat();
char ch=dis.readChar();
boolean b=dis.readBoolean();
String str= dis.readUTF();
System.out.println(i);
System.out.println(f);
System.out.println(ch);
System.out.println(b);
System.out.println(str);
}
}
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.