Sunday 24 June 2012

Using DataInputStream and DataOutputStream in Java

A data input stream lets an application read primitive Java data types from an underlying input stream in a machine-independent way. An application uses a data output stream to write data that can later be read by a data input stream.

The data input stream is implemented using the class java.io.DataInputStream and data output stream is implemented using the class java.lang.DataOutputStream. Both of this class have the corresponding method to write primitive data and read it back.Using this class make it easier to read integer, float, double data and others without needing to interpret if the read data should be an integer or a float data. Let us discuss about these two classes



Class: public class DataInputStream extends FilterInputStreamimplements DataInput

Constructor: public DataInputStream(InputStream in)

Creates a DataInputStream that uses the specified underlying InputStream.


DataInputStream provided methods for reading primitive data types.



Return Value
Method
boolean
readBoolean()
Reads one input byte and returns true if that byte is nonzero, false if that byte is zero.
byte
readByte()
Reads and returns one input byte.
char
readChar()
Reads an input char and returns the char value.
double
readDouble()
Reads eight input bytes and returns a double value.
float
readFloat()
Reads four input bytes and returns a float value..
int
readInt()
Reads four input bytes and returns an int value.
long
readLong()
 Reads eight input bytes and returns a long value.
short
readShort()
Reads two input bytes and returns a short value.


Class: public class DataOutputStream extends FilterOutputStream implements DataOutput

Constructor: public DataOutputStream(OutputStream out)

Creates a new data output stream to write data to the specified underlying output stream. The counter written is set to zero.


DataOutputStream provided methods for writing primitive data types.


Return Value
Method
void
writeBoolean(boolean v)
Writes a
boolean to the underlying output stream as a 1-byte value.
void
writeByte(int v)
Writes out a
byte to the underlying output stream as a 1-byte value.
void
writeChar(int v)
Writes a
char to the underlying output stream as a 2-byte value, high byte first.
void
writeDouble(double v)Converts the double argument to along using the doubleToLongBits method in class Double, and then writes that long value to the underlying output stream as an 8-byte quantity, high byte first.
void
writeFloat(floatv)
Converts the float argument to an
int using the floatToIntBits method in class Float, and then writes that int value to the underlying output stream as a 4-byte quantity, high byte first.
void
writeInt(int v)
Writes an
int to the underlying output stream as four bytes, high byte first.

writeLong(long v)
Writes a
long to the underlying output stream as eight bytes, high byte first.
void
writeShort(int v)
Writes a
short to the underlying output stream as two bytes, high byte first.



Example :


The following code writes five students data (rollno, maths mark, science mark,avg. mark and pass/fail) in to a file stu.dat and reads it one by one and displays it.

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class StoreStudentDataInFile {

 /**
  * @param Pass
  * @throws IOException
  **/
 public static void main(String[] args) {

  // Prepares some data to be written to a file

  int rollno[] = { 100, 101, 102, 103, 104, 105 };
  int mathsMark[] = { 51, 60, 71, 80, 90 };
  int sciMark[] = { 70, 30, 50, 90, 80 };
  float avgMark[] = { 60.5f, 45, 60.5f, 85, 85 };
  boolean passOrFail[] = { true, false, true, true, true };

  DataInputStream in = null;
  DataOutputStream out = null;

  try {
   out = new DataOutputStream(new FileOutputStream(
     "/home/radhakrishnan/Desktop/stu.dat"));
   in = new DataInputStream(new FileInputStream(
     "/home/radhakrishnan/Desktop/stu.dat"));

   // Writing students data to the file

   for (int i = 0; i < 5; i++) {

    out.writeInt(rollno[i]);
    out.writeInt(mathsMark[i]);
    out.writeInt(sciMark[i]);
    out.writeFloat(avgMark[i]);
    out.writeBoolean(passOrFail[i]);
   }

   // Reading students data from file

   for (int i = 0; i < 5; i++) {
    System.out.println("Student:" + (i + 1));
    System.out.println("Rollno:" + in.readInt());
    System.out.println("Maths Mark:" + in.readInt());
    System.out.println("Science Mark:" + in.readInt());
    System.out.println("Avg Mark:" + in.readFloat());
    System.out.println("Is Pass?:" + in.readBoolean());

   }

  } catch (IOException e) {
   System.out.println(e);
  } finally {
   try {
    in.close();
    out.close();
   } catch (IOException e) {
    e.printStackTrace();
   }
  }
 }
}


Output


Student:1
Rollno:100
Maths Mark:51
Science Mark:70
Avg Mark:60.5
Is Pass?:true
Similarly it will display the details of remaining four students.

No comments:

Post a Comment