diff --git a/Herd.java b/Herd.java index 200d05a..bf6a527 100755 --- a/Herd.java +++ b/Herd.java @@ -13,6 +13,7 @@ import java.util.ArrayList; import java.util.Random; +import java.io.*; public class Herd { @@ -119,14 +120,35 @@ if (o >= 0) offspring[o] = null; } + //TODO Test the serialization code! + public void saveHerd(String filename) { - //TODO save to binary file + try { + FileOutputStream fileOut = new FileOutputStream(filename); + ObjectOutputStream out = new ObjectOutputStream(fileOut); + out.writeObject(this); + out.close(); + fileOut.close(); + } catch (IOException i) { + i.printStackTrace(); + } } - public void loadHerd(String filename) + public static void loadHerd(String filename) { - //TODO load from binary file + Herd singleton = null; + try { + FileInputStream fileIn = new FileInputStream(filename); + ObjectInputStream in = new ObjectInputStream(fileIn); + singleton = (Herd) in.readObject(); + in.close(); + fileIn.close(); + } catch (IOException i) { + i.printStackTrace(); + } catch (ClassNotFoundException c) { + c.printStackTrace(); + } } public void resetHerd()