// Make sure your classes implement java.io.Serializable class Entry implements Serializable { String mydata; } String FILENAME = "/tmp/stuff.dat"; boolean writeStuff(ArrayList<Entry> list) { try { ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(FILENAME)); oos.writeObject(list); oos.flush(); oos.close(); } catch(Exception ex) { System.out.println("Could not write stuff because " + ex.toString()); return false; } return true; } @SuppressWarnings("unchecked") ArrayList<Entry> readStuff() { ArrayList<Entry> list = new ArrayList<Entry>(); try { ObjectInputStream ois = new ObjectInputStream(new FileInputStream(FILENAME)); list = (ArrayList<Entry>) ois.readObject(); ois.close(); } catch(Exception ex) { System.out.println("Could not read stuff because " + ex.toString()); } return list; }
Programming Tips - Java: Quickly save a data structure to disk
Date: 2015mar30
Language: Java
Q. Java: Quickly save a data structure to disk
A. Use Java serialization, like this: