java中的序列化

来源:互联网 发布:监控windows 资源命令 编辑:程序博客网 时间:2024/05/21 04:38
序列化就是一种用来处理对象流的机制,所谓对象流也就是将对象的内容进行流化。可以对流化后的对象进行读写操作,也可将流化后的对象传输于网络之间。序列化是为了解决在对对象流进行读写操作时所引发的问题。
序列化的实现:将需要被序列化的类实现Serializable接口,该接口没有需要实现的方法,implements Serializable只是为了标注该对象是可被序列化的,然后使用一个输出流(如:FileOutputStream)来构造一个 ObjectOutputStream(对象流)对象,接着,使用ObjectOutputStream对象的writeObject(Object obj)方法就可以将参数为obj的对象写出(即保存其状态),要恢复的话则用输入流。

简单来说 序列化就是把Java对象储存在某一地方(硬盘、网络),以便于传输。

代码示例:

package com.java.oop119;import java.io.Serializable;public class GirlFriend implements Serializable {private String name;private int age;public GirlFriend(String name, int age) {super();this.name = name;this.age = age;}public GirlFriend() {super();}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}@Overridepublic String toString() {return "GirlFrien [name=" + name + ", age=" + age + "]";}}


package com.java.oop119;import java.io.Serializable;//类通过实现 java.io.Serializable 接口以启用其序列化功能。未实现此接口的类将无法使其任何状态序列化或反序列化。//可序列化类的所有子类型本身都是可序列化的。序列化接口没有方法或字段,仅用于标识可序列化的语义。public class Man implements Serializable {private String name;private int age;private GirlFriend girl;public Man(String name, int age, GirlFriend girl) {super();this.name = name;this.age = age;this.girl = girl;}public Man() {super();}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public GirlFriend getGirl() {return girl;}public void setGirl(GirlFriend girl) {this.girl = girl;}@Overridepublic String toString() {return "Man [name=" + name + ", age=" + age + ", girl=" + girl + "]";}}


package com.java.oop119;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;import java.util.ArrayList;import java.util.List;public class Test {public static void main(String[] args) {Man m1 = new Man("小强", 20, new GirlFriend("小花", 18));Man m2 = new Man("英雄", 28, new GirlFriend("美女", 20));Man m3 = new Man( "校草", 22, new GirlFriend("校花", 17));FileOutputStream out =null;//对象流输入到文件ObjectOutputStream objOut=null;//对象输入流成对出现FileInputStream in = null;//从文件中读取到工程ObjectInputStream objIn =null;try {out = new FileOutputStream("man/man.properties");objOut = new ObjectOutputStream(out);in = new FileInputStream("man/man.properties");objIn = new ObjectInputStream(in);List<Man> lit  =new ArrayList<Man>();lit.add(m1);lit.add(m2);lit.add(m3);objOut.writeObject(lit);//输入对象到文件中List<Man> newLit = (List<Man>) objIn.readObject();//从文件中输入存储到新集合中System.out.println(newLit);objOut.flush();//刷新输出流} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} catch (ClassNotFoundException e) {e.printStackTrace();}finally{try {if(objIn!=null){objIn.close();}if(objOut!=null){objOut.close();}} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}


import java.io.FileOutputStream;import java.io.IOException;import java.util.Properties;public class Learn1 {public static void main(String[] args) {//PropertiesProperties pro = new Properties();// 增--注意键值对都是String类型pro.setProperty("MrLi", "666");pro.setProperty("small pang!", "2333");pro.setProperty("big pang!", "I No care~");System.out.println(pro);// 删--remove(键),如果有则删除,如果没有则没有效果pro.remove("小黎1");System.out.println(pro);// 查--getProperty(键) 得到键的值,如果没有该键,则返回nullString str = pro.getProperty("小黎1");System.out.println(str);// 改--同样使用setProperty()方法pro.setProperty("MrLi", "6666");System.out.println(pro);try {pro.store(new FileOutputStream("test/xiaoli.properties"), null);System.out.println("存储成功");} catch (IOException e) {e.printStackTrace();}}}



原创粉丝点击