Java Example: Using Binary File

To write codes that use Binary file, we need to apply Serialization class. Basically, java with a binary files are involving with the objects. Therefore we should at least have a java class (one java class).

How to use Serialization?
The codes:
Location:
  1) import java.io.*;
  2) Implement java.io.Serialization interface
java class
  1) Use writeObject and readObject methods whose header are as
      given below:
     
       a) void writeObject (Object obj) throws IOException;
       b) Object readObject() throws ClassNotFoundException,
              IOException;
java application



The example below, shows a java application. This java application applies object from Class Student. The program code to do the following tasks:

  1. store the data into Student.txt using serializable (store object data into file)
  2. read data (objet from Student.txt)
  3. get the specific data, then store into text file (CS225.txt)

//Create the binary object file and text files
import java.io.*;
import java.util.Scanner;

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

       Scanner scanner = new Scanner(System.in);  

       try
       {
           File outfile = new File("Student.txt");       //create the output binary file
           FileOutputStream outStream = new FileOutputStream(outfile);
           ObjectOutputStream objOutStream = new ObjectOutputStream(outStream);
   
           File infile = new File("Student.txt");        //the input binary file to be read from
           FileInputStream inputstream = new FileInputStream(infile);
           ObjectInputStream objInStream = new ObjectInputStream(inputstream);

           //to create the output text files
           FileWriter outfilewriter1 = new FileWriter("CS225.txt");
           BufferedWriter bufwriter1 = new BufferedWriter(outfilewriter1);
           PrintWriter printwriter = new PrintWriter(bufwriter1);


       //1) get input for 500 students  and store into file (using object serializable) 
           for (int i=0; i < 500; i++)
           {
               System.out.print("\n noID : ");
               String noid = scanner.next();
               System.out.print("\n Name : ");
               String name= scanner.next();
               System.out.print("\n Program Code : ");
               String programcode = scanner.next();
               System.out.print("\n Program : ");
               double program= scanner.nextDouble();
               System.out.print("\n Number of Subjects : ");
               int noSubject = scanner.nextInt();
               System.out.print("\n Cost : ");
               double cost = scanner.nextDouble();
              
               Student stud = new Student(noid, name, programcode, program, noSubject, cost);   //create the    Student object
              
               objOutStream.writeObject(stud);     //write the Student object created into binary bile named Student.txt
            }

        //2 ) to read from the binary file named Student.txt and output to text files named CS225.txt
       
            Student temp;
            for (int i=0; i < 500; i++)
            {
                temp = (Student) objInstream.readObject();      //casting to access the members of the class Student
               
              //3) to write to the text files named CS225 depending on the program code 
                if(temp.getProgramCode().equalsIgnoreCase("CS225"))
                {
                        printwriter.println("No Id: + "temp.getNoid());
                        printwriter.println("Name: + "temp.getName());
                        printwriter.println("Program Code: + "temp.getProgramCode());
                        printwriter.println("Program: + "temp.getProgram());
                        printwriter.println("Number of Subject: + "temp.getNumberofSubject());
                        printwriter.println("Cost: + "temp.getCost());
                }
              
            }
           
       
         objOutStream.close(); 
         objInStream.close();
         printwriter.close();

       }
    catch(ClassNotFoundExeption cnfe)
    {
      System.out.println(cnfe.getMessage());
    }
    catch(FileNotFoundExeption fnfe)
    {
         System.out.println(fnfe.getMessage());
    }
    catch(IOExeption ioe)
    {
       System.out.println(ioe.getMessage());   
    }

   }
}       

No comments: