package framework; import java.io.*; /** Responsible for saving the model to a file and for reading the model from a file.
This is used by the FileMenuController, but could be used in other places, too. */ public class FileSystemGateway { /** Reads a model from a specified file. @param fname The name of the file containing the model. @return The saved model */ public Model getModel(String fname) throws AppError { try { ObjectInputStream ois = new ObjectInputStream(new FileInputStream(fname)); Model model = (Model) ois.readObject(); model.setFname(fname); // just in case ois.close(); return model; } catch(Exception e) { throw new AppError(e.getMessage()); } } /** Saves a model to a specified file. @param model the model to be saved. @param fname the name of the specified file */ public void saveModelAs(Model model, String fname) throws AppError { try { model.setUnsavedChanges(false); model.setFname(fname); // needed? ObjectOutputStream obstream = new ObjectOutputStream( new FileOutputStream(fname)); obstream.writeObject(model); obstream.flush(); obstream.close(); } catch(Exception e) { throw new AppError(e.getMessage()); } } /** Same as saveModelAs(m, m.getFname()); @param m the model to be saved. */ public void saveModel(Model m) throws AppError { saveModelAs(m, m.getFname()); } }