首页 > 代码库 > java序列化
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序列化
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。