
/**
 * BinaryFileManager.java
 * This class handels the input and output of binary files for the LabFile 
 * program.
 *
 * Created: Sat Jun 24 11:05:12 2000
 *
 * @author Nathan Stevens
 * @version 0.11 03/12/06
 */
import java.io.*;

public class BinaryFileManager {
  
  // Method to read an object from a file
  public static Object getObjectFromFile(File file) {
    try {	
      // Create the output stream object
      ObjectInputStream in = 
        new ObjectInputStream(
        new BufferedInputStream(
        new FileInputStream(file)));
      
      Object object =  in.readObject();
      in.close();
      
      return object;
    }
		catch(IOException e) {
      System.err.println("The following error ocurred in the BinaryFileManager\n" + e);          
		}
		catch(ClassNotFoundException e) {
			System.err.println("The following error occured in the BinaryFileManager\n" + e);
		}
		// if we catched an exception just return null
    return null;
	}
  
  // Method to save an object to a file
  public static void saveObjectToFile(Object object, File file) {
		try {
			// Create the object ouput stream
      ObjectOutputStream out = 
				new ObjectOutputStream(
				new BufferedOutputStream(
        new FileOutputStream(file)));
      
			out.writeObject(object);
			out.close(); 
		}
		catch(IOException e) {
			System.err.println("IO exception in the BinaryFileManager\n" + e);
		}
  }
} // --end class BinaryFileManager
