/** * This method saves the current MOCOData into a serialized file * * @param saveAs The name of the outputfile */ public void saveObject(String saveAs) { File sFile = new File(saveAs); try { ObjectOutputStream oo = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(sFile))); oo.writeObject(this.state); oo.close(); } catch (Exception ex) { if (this.mainFrame != null) { JOptionPane.showMessageDialog( this.mainFrame, "Couldn't write to file: " + sFile.getName() + "\n" + ex.getMessage(), "Save object", JOptionPane.ERROR_MESSAGE); } else { System.out.println("Couldn't write to file: " + sFile.getName() + "\n" + ex.getMessage()); } } }
/** * This methods loads the current state of MOCO from a serialized file * * @param loadFrom The name of the serialized file * @return The new state of MOCO */ public Object openObject(String loadFrom) { File selected = new File(loadFrom); try { ObjectInputStream oi = new ObjectInputStream(new BufferedInputStream(new FileInputStream(selected))); Object obj = oi.readObject(); oi.close(); if (!(obj instanceof MOCCOState)) { throw new Exception("Object not of type MOCCOState"); } return obj; } catch (Exception ex) { if (this.mainFrame != null) { JOptionPane.showMessageDialog( this.mainFrame, "Couldn't read object: " + selected.getName() + "\n" + ex.getMessage(), "Open object file", JOptionPane.ERROR_MESSAGE); } else { System.out.println("Couldn't read object: " + selected.getName() + "\n" + ex.getMessage()); } } return null; }