/** JDK before version 7. */ @SuppressWarnings("unchecked") public static void main(String[] args) { // create a Serializable List List<String> quarks = Arrays.asList("up", "down", "strange", "charm", "top", "bottom");
// serialize the List // note the use of abstract base class references try { // use buffering OutputStream file = new FileOutputStream("quarks.ser"); OutputStream buffer = new BufferedOutputStream(file); ObjectOutput output = new ObjectOutputStream(buffer); try { output.writeObject(quarks); } finally { output.close(); } } catch (IOException ex) { // ex.printStackTrace(); System.out.println("Cannot perform output: " + ex); }
// deserialize the quarks.ser file // note the use of abstract base class references try { // use buffering InputStream file = new FileInputStream("quarks.ser"); InputStream buffer = new BufferedInputStream(file); ObjectInput input = new ObjectInputStream(buffer); try { // deserialize the List List<String> recoveredQuarks = (List<String>) input .readObject(); // display its data for (String quark : recoveredQuarks) { System.out.println("Recovered Quark: " + quark); } } finally { input.close(); } } catch (ClassNotFoundException ex) { // ex.printStackTrace(); System.out.println("Cannot perform input. Class not found: " + ex); } catch (IOException ex) { ex.printStackTrace(); } } }