Object I/O

This application illustrates the written and read of Java Objects to and from a file.

You should look closely at the ObjectOutputStream and the Calendar classes.

MessageObject

import java.util.*;
import java.io.*;

class MessageObject implements Serializable{

    private String string;
    private int number;
    private Date date;
    private Calendar calendar;

    public MessageObject ()  {
        
      date = new Date();
      calendar = Calendar.getInstance();
      calendar.setTime ( date );
      
      string = new String ( "null" );
      number = 0;
    }  // end Message constructor

    public void setString( String s )  {
        string = s;
    }  // end setString

    public String getString()  {
        return string;
    }  // end getString

    public void setNumber( int i )  {
        number = i;
    }  // end setNumber

    public int getNumber()  {
        return number;
    }  // end getNumber

    public void setTime( Calendar c )  {
        calendar = c;
    }  // end setTime

    public Calendar getTime()  {
        return calendar;
    }  // end getTime
    
    public String getTodaysDate()  {
        
        String s = new String();
        
        s += calendar.get(Calendar.MONTH) + "/";
        s += calendar.get(Calendar.DATE) + "/";
        s += calendar.get(Calendar.YEAR) + ": ";
        s += calendar.get(Calendar.HOUR) + ":";
        s += calendar.get(Calendar.MINUTE) + ":";
        s += calendar.get(Calendar.SECOND) + " ";
        
        return s;

    }  // end getTime


}  // end MessageObject

Object IO Application

import java.awt.*;
import java.io.*;
import java.util.*;

public class ObjectIO {

//  simplest write/read of an Object to/from a file


  public ObjectIO  ()  {
    super ();
  }  // end ObjectIO  constructor

  public static void main ( String [ ] args )  {

  File ioFile;
  FileOutputStream outFileStream;
  ObjectOutputStream outObjectStream;
  FileInputStream inFileStream;
  ObjectInputStream inObjectStream;

  String fileName;
  String path;

  MessageObject outMessageObject, inMessageObject;
  
  ObjectIO objectIO = new ObjectIO ();
  
  outMessageObject = new MessageObject();

  System.out.println ( "\n\nBefore writing object:");
  System.out.println ( "\n  Message.string = " + outMessageObject.getString() );
  System.out.println ( "  Message.number = " + outMessageObject.getNumber() );
  System.out.println ( "  Message.time.millisecond = " + (outMessageObject.getTime()).get( Calendar.MILLISECOND ) );

  outMessageObject.setString ( "Hello Comp118 class!" );
  outMessageObject.setNumber ( 4 );

  // Instantiate the file
  if ( args.length > 0 ) fileName = args[0];
  else {
    fileName = new String ( "testfile" );
    path = new String ( "." );
  }
  if ( args.length > 1 ) path = args[1];
  else path = new String ( "." );

  // Instantiate the file object
  ioFile = new File ( path, fileName );

  // Instantiate and chain the FileOutputStream
  try {
    outFileStream = new FileOutputStream ( ioFile );
    outObjectStream = new ObjectOutputStream ( outFileStream );
    inFileStream = new FileInputStream ( ioFile );
    inObjectStream = new ObjectInputStream ( inFileStream );
    }  // end try
  catch ( IOException except ) {
    return;
    }  // end catch

  try {
    System.out.println ( "\n  Writing to file: " + ioFile.getName());
    outObjectStream.writeObject ( outMessageObject );
    outObjectStream.flush ();
    outObjectStream.close ();

     System.out.println ( "\n\nAfter reading object:");
     System.out.println ( "\n  Reading from file: " + ioFile.getName() );
    inMessageObject = (MessageObject)inObjectStream.readObject ( );
  }
  catch ( IOException e ) {
    System.out.println ( "Error writing MessageObject " + e.toString() );
    return;
    }  // end catch
  catch ( ClassNotFoundException e ) {
    System.out.println ( "Error: ClassNotFoundException " + e.toString() );
    return;
    }  // end catch


  // messages to user
  System.out.println ( "\n  Message.string = " + inMessageObject.getString() );
  System.out.println ( "  Message.number = " + inMessageObject.getNumber() );
  System.out.println ( "  Message.time.millisecond = " + (inMessageObject.getTime()).get( Calendar.MILLISECOND ) );
  System.out.println ( "  Current time.millisecond = " + (Calendar.getInstance()).get( Calendar.MILLISECOND ) );

  System.out.println ( "\n  Current Calendar object = " + Calendar.getInstance() );

  System.out.println ( "\n  Today\'s date: " + inMessageObject.getTodaysDate() );
  }  // end main

}  // end ObjectIO