Monday, 3 June 2013

Java File and IO : DataOutputStream

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 DataOutputStream in writing boolean data type

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

public class DataOutputStreamWriteBoolean {
   public static void main(String[] args) {
      String file = "C://tutorialpointph//booleanfile.txt";    
      try
      {
  //create the file booleanfile
  FileOutputStream fos = new FileOutputStream(file);
      
  /*    
  * DataOutputStream(OutputStream os) constructor.   
  */
      
  DataOutputStream dos = new DataOutputStream(fos);      
  boolean b = true; //One byte of data
      
  /*
  * writeBoolean() methods writes the boolean to output 
  * as a byte if true. If false, no byte is present.
  */
        
   dos.writeBoolean(b);
       
   System.out.println("boolean byte has been written.");
   dos.close();
        
      }  
      catch (IOException e)
      {
   System.out.println("IOException : " + e);
      }
   }
}

Using DataOutputStream in writing byte data type

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);
     }
 }
}

Java File and IO: BufferedOutputStream

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:

BufferedOutputStream

import java.io.BufferedOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class BufferedOutputStreamExample {
 public static void main(String[] args) {
  String file = "C://tutorialpointph//file.txt";
  BufferedOutputStream bos = null;
         
        try
        {
            //Get the file to write data to 
            FileOutputStream fos = new FileOutputStream(file);
                 
            /*
             * creates a BufferedOutputStream          
             */  
            bos = new BufferedOutputStream(fos);
                 
            char a = 'a';  
            bos.write(a);
            System.out.println("Char 'a' has been written.");
         }
         catch(FileNotFoundException e){
             System.out.println("File not found on path" + e);
         }
         catch(IOException ie){
             System.out.println("Exception while writing" + ie);
          }
         finally
         {
              try
              {
                 if(bos != null)
                 {
                    bos.flush();
                    bos.close();
                 }
               }
               catch(Exception e){}
         }
 }
}

Java File and IO: BufferedInputStream

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:

BufferedInputStream

/*
* Example of Buffered Input Stream
*/
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class BufferedInputStreamExample {
 public static void main(String[] args) {
           
  //Get the file to read using BufferedInputStream
        File file = new File("C://tutorialpointph//file.txt");
        BufferedInputStream bin = null;
         
        try
        {
            //obtains input bytes from the file
            FileInputStream fin = new FileInputStream(file);                  
            /*
            * creates a BufferedInputStream and saves 
            * it into the buffer array for further use
            */                  
            bin = new BufferedInputStream(fin);                                      
            //read the file 
            //available : while there are bytes to read
            while( bin.available() > 0 ){                        
             System.out.print((char)bin.read());
            }                  
         }
         catch(FileNotFoundException e){
            System.out.println("File not found on path" + e);
         }
         catch(IOException ie)
         {
            System.out.println("Exception while reading " + ie); 
           }
         finally
         {
            //close the BufferedInputStream after reading
            try{
              if(bin != null)
              bin.close();                                  
             }catch(IOException ie){
              System.out.println("Error while closing : " + ie);
             }                  
         }
   }
}