Monday, 3 June 2013

Java File and IO: DataInputStream

There are a lot of APIs regarding files and IO in Java. Some of the basics will be discussed here.
  1. BufferedInputStream
  2. BufferedOutputStream
  3. DataInputStream
  4. DataOutputStream
First let us differentiate all these.

1. BufferedInputStream :  kind of inputStream that buffers the input data in order to optimize the speed access to the data.

2. BufferedOutputStream : kind of outputStream that buffers bytes.

3. DataInputStream : is a kind of InputStream used to read data as primitive data types from an underlying input stream.

4.DataOutputStream : kinf of outputStream that allows user to write data as primitive data types.

Examples:

Using DataInputStream in reading boolean data type

import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class DataInputStreamReadBoolean {
 public static void main(String[] args){

 String file = "C://tutorialpointph//booleanfile.txt";    
     try
     {
       //Get the file to read using BufferedInputStream
       FileInputStream fin = new FileInputStream(file);
      
       /*
        * DataInputStream(InputStream in) constructor.
        */      
        DataInputStream din = new DataInputStream(fin);
      
        /*
         * readBoolean() method reads a byte from the file
         * and return true if the byte is not zero and 
         * false if byte is zero.     
         */
        
         boolean b = din.readBoolean();        
         System.out.println("Value on text file : " + b);        
   
         din.close();
        
     }
     catch(FileNotFoundException e)
     {
       System.out.println("File not found on path : " + e);
     }
     catch(IOException ie)
     {
       System.out.println("Exception while reading : " + ie);
     }
 }
}


Using DataInputStream in reading byte data type

import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class DataInputStreamReadByte {
 public static void main(String[] args){
  String file = "C://tutorialpointph//bytefile.txt";    
     try
     {
       //Get the file to read using BufferedInputStream
       FileInputStream fin = new FileInputStream(file);
      
      /*
       * DataInputStream(InputStream in) constructor.
        */      
        DataInputStream din = new DataInputStream(fin);
      
        /*
         * readByte() method reads a byte from the file         
         */
        
         byte b = din.readByte();        
         System.out.println("Value on text file : " + b);        
   
         din.close();
        
     }
     catch(FileNotFoundException e)
     {
       System.out.println("File not found on path : " + e);
     }
     catch(IOException ie)
     {
       System.out.println("Exception while reading : " + ie);
     }
 }
}

Using DataInputStream in reading int data type

import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class DataInputStreamReadInt {
 public static void main(String[] args){
  String file = "C://tutorialpointph//intfile.txt";    
     try
     {
       //Get the file to read using BufferedInputStream
       FileInputStream fin = new FileInputStream(file);
      
       /*
        * DataInputStream(InputStream in) constructor.
        */      
        DataInputStream din = new DataInputStream(fin);
      
        /*
         * readInt() method reads an integer primitive
                * from the file         
         */
        
         int i = din.readInt();        
         System.out.println("Value on text file : " + i);        
   
         din.close();
        
     }
     catch(FileNotFoundException e)
     {
       System.out.println("File not found on path : " + e);
     }
     catch(IOException ie)
     {
       System.out.println("Exception while reading : " + ie);
     }
 }
}

0 comments:

Post a Comment