Friday, December 3, 2010

Program that will demonstrate the use of object serialization with different write methods

/* Program : 1
   Create an application that will demonstrate the use of object serialization with different write methods
   Developed by : Malhar Vora
   Developed on :6-11-2010
   Development Status : Completed and tested
   Email     : vbmade2000@gmail.com
   WebSite   : www.malhar2010.blogspot.com
********************************************************************************/
import java.io.*;

public class P1 
{
 
 public static void main(String []str)
 {
  
  ReadWriteData rwdata = new ReadWriteData();
  rwdata.writeData("test1.txt");
  rwdata.readData("test1.txt"); 
 
 
 }

}

class ReadWriteData
{
 public void writeData(String fname)
 {
  FileOutputStream fos = null;
  ObjectOutputStream oos = null;
  
  try{
  
    fos = new FileOutputStream(new File(fname));
    oos = new ObjectOutputStream(fos);
    
    oos.writeInt(10);
    oos.writeUTF("Hello World");
  
  
  }
  catch(Exception e)
  {
   
  }
  finally
  {
   try
   {
    oos.close();
    fos.close();
   }
   catch(Exception e)
   {
    ;
   }
  }
  
 }
 
 public void readData(String fname)
 {
  FileInputStream fis = null;
  ObjectInputStream ois = null;
  
  try{
  
    fis = new FileInputStream(new File(fname));
    ois = new ObjectInputStream(fis);
    
    System.out.println("Integer " + ois.readInt());
    System.out.println("String " + ois.readUTF());
  
  
  }
  catch(Exception e)
  {
   ;
  }
  finally
  {
   try
   {
    ois.close();
    fis.close();
   }
   catch(Exception e)
   {
    ;
   }
  }
  
 }
 
 

}

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.