java序列化
Posted 你还需要努力
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java序列化相关的知识,希望对你有一定的参考价值。
/* *java简单序列化 */ public class Main{ public static void main(String[] args){ File file = new File("person.out"); try{ ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(file)); Person person = new Person("John",101,Gender.MALE); out.writeObject(person); out.close(); System.out.println(Gender.MALE); ObjectInputStream oin = new ObjectInputStream(new FileInputStream(file)); Object newPerson = oin.readObject(); oin.close(); System.out.println(newPerson); }catch(Exception e){ e.printStackTrace(); } } } enum Gender{ MALE,FEMALE } class Person implements Serializable{ private String name=null; private Integer age=null;//transient默认序列化忽略该字段 private Gender gender=null; public Person(){ System.out.println("none-argconstructor"); } public Person(String name,Integer age,Gender gender){ System.out.println("argconstructor"); this.name=name; this.age=age; this.gender=gender; } @Override public String toString(){ return"["+name+","+age+","+gender+"]"; } public String getName(){ return name; } public void setName(String name){ this.name=name; } public Integer getAge(){ return age; } public void setAge(Integer age){ this.age=age; } public Gender getGender(){ return gender; } public void setGender(Gender gender){ this.gender=gender; } }
以上是关于java序列化的主要内容,如果未能解决你的问题,请参考以下文章