import java.io.*; //标识此类对象可以被序列化 public class ObjectSerializable implements Serializable{ private String name; private int price; public ObjectSerializable(String name, int price){ this.name = name; this.price = price; } @Override public String toString() { return "名称:" + this.name + "价格:" + this.price; } //序列化 public static void ser() throws Exception{ ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(new File("./o1.txt"))); oos.writeObject(new ObjectSerializable("Java开发",99)); oos.close(); } //反序列化 public static void dser()throws Exception{ ObjectInputStream ois = new ObjectInputStream(new FileInputStream(new File("./o1.txt"))); Object obj = ois.readObject(); ObjectSerializable info = (ObjectSerializable) obj; System.out.println(info); ois.close(); } public static void main(String[] args) throws Exception{ ser(); dser(); } }
09-15
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交