Details of DataInputStream and DataOutputStream Class?

java.io.DataInputStream class:

  • DataInputStream class is a concrete subclass of all input stream classes.
  • DataInputStream class is used for 2 purposes. They are:
    • Reading the data from input device called keyboard.
    • It is also used for reading the data from other machine which is located in other network.
  • Reading the data either locally or globally is nothing but creating an object of DataInputStream class.
  • DataInputStream class API:
    • Constructor: DataInputStream (InputStream)
    • InputStream is an abstract class whose object cannot be created directly but it can be created indirectly.
    • An object of InputStream class called “in” which is created as a static data member in System class, Hence an object of InputStream class is “System.in”.
    • For example: DataInputStream dis = new DataInputStream (System.in);
  • Instance 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 readLine();

Note: public void readLine() method is used for reading any kind of data and it is available in the form of String object.

 

Example:

import java.io.*;
class DataInputStreamDemo
{
		public Static void main(String args[]) throws Exception 
{
DataInputStream dis = new DataInputStream (System.in);

System.out.println(“Enter first value : ”);
int n1= Integer.parseInt(dis.readLine());

System.out.println(“Enter second value : ”);
int n2=  Integer.parseInt(dis.readLine());

int n =n1+n2;
System.out.println(“Sum of”+n1+”and ”+n2+”:”+n3); 
}
}


java.io.DataOutputStream class:

  • DataOutputStream class is a concrete subclass of all output stream classes.
  • DataOutputStream is used 2 purposes. They are:
    1. To display the data onto the console.
    2. Writing the data on some other machine which is located on some other network.
  • To achieve the above purpose, we need to create an object of DataOutputStream class.
  • DataOutputStream class API:
    • Constructor: DataOutputStream (OutputStream)
    • OutputStream is an abstract class whose object cannot be created directly but it can be created indirectly.
    • An object of OutputStream class called “out” which is created as a static data member in System class, Hence an object of InputStream class is “System.out”.
    • For example: DataOutputStream dos = new DataOutputStream (System.out);
  • Instance Methods:
    • public void write(byte);
    • public void write(short);
    • public void write(int);
    • public void write(long);
    • public void write(float);
    • public void write(double);
    • public void write(char);
    • public void write(boolean);

 

  • public void writeByte(byte);
  • public void writeShort(short);(
  • public void writeInt(int);
  • public void writeLong(long);
  • public void writeFloat(float);
  • public void writeDouble(double);
  • public void writeChar(char);
  • public void writeBoolean(boolean);

										  

Leave a Reply

Your email address will not be published. Required fields are marked *