您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
java序列化与ObjectOutputStream和ObjectInputStream的实例详解
一个测试的实体类:
public class Param implements Serializable { private static final long serialVersionUID = 5187074869820982336L; private Integer param1; private String param2; public Integer getParam1() { return param1; } public void setParam1(Integer param1) { this.param1 = param1; } public String getParam2() { return param2; } public void setParam2(String param2) { this.param2 = param2; } }
测试:
public class Main { public static void main(String[] args) throws Exception { SerializeParam(); Param param = DeserializeParam(); System.out.println(MessageFormat.format("param1={0},param2={1}", param.getParam1(), param.getParam2())); } /** * 将实体类序列化到本地 * @throws FileNotFoundException * @throws IOException */ private static void SerializeParam() throws FileNotFoundException, IOException { Param param = new Param(); param.setParam1(123); param.setParam2("asdf"); ObjectOutputStream oo = new ObjectOutputStream(new FileOutputStream( new File("E:/param.txt") )); oo.writeObject(param); System.out.println("Person对象序列化成功!"); oo.close(); } /** * 反序列化 * @return * @throws Exception * @throws IOException */ private static Param DeserializeParam() throws Exception, IOException { ObjectInputStream ois = new ObjectInputStream(new FileInputStream( new File("E:/param.txt"))); Param param = (Param) ois.readObject(); System.out.println("Person对象反序列化成功!"); ois.close(); return param; } }
如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望通过本文能帮助到大家,谢谢大家对本站的支持!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。