欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

Java 序列化 (Serializable) 的例子,序列化serializable,SimpleSerial

来源: javaer 分享于  点击 44673 次 点评:153

Java 序列化 (Serializable) 的例子,序列化serializable,SimpleSerial


SimpleSerialization.java

public class SimpleSerialization {    /**     * Create a simple Hashtable and serialize it to a file called     * HTExample.ser.     */    private static void doSave() {        System.out.println();        System.out.println("+------------------------------+");        System.out.println("| doSave Method                |");        System.out.println("+------------------------------+");        System.out.println();        Hashtable h = new Hashtable();        h.put("string", "Oracle / Java Programming");        h.put("int", new Integer(36));        h.put("double", new Double(Math.PI));        try {            System.out.println("Creating File/Object output stream...");            FileOutputStream fileOut = new FileOutputStream("HTExample.ser");            ObjectOutputStream out = new ObjectOutputStream(fileOut);            System.out.println("Writing Hashtable Object...");            out.writeObject(h);            System.out.println("Closing all output streams...\n");            out.close();            fileOut.close();        } catch(FileNotFoundException e) {            e.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        }    }    /**     * Loads the contents of a previously serialized object from a file called     * HTExample.ser.     */    private static void doLoad() {        System.out.println();        System.out.println("+------------------------------+");        System.out.println("| doLoad Method                |");        System.out.println("+------------------------------+");        System.out.println();        Hashtable h = null;        try {            System.out.println("Creating File/Object input stream...");            FileInputStream fileIn = new FileInputStream("HTExample.ser");            ObjectInputStream in = new ObjectInputStream(fileIn);            System.out.println("Loading Hashtable Object...");            h = (Hashtable)in.readObject();            System.out.println("Closing all input streams...\n");            in.close();            fileIn.close();        } catch (ClassNotFoundException e) {            e.printStackTrace();        } catch(FileNotFoundException e) {            e.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        }        System.out.println("Printing out loaded elements...");        for (Enumeration e = h.keys(); e.hasMoreElements(); ) {            Object obj = e.nextElement();            System.out.println("  - Element(" + obj + ") = " + h.get(obj));        }        System.out.println();    }    /**     * Sole entry point to the class and application.     * @param args Array of String arguments.     */    public static void main(String[] args) {        doSave();        doLoad();    }}
相关栏目:

用户点评