标签:java序列化 序列化 反序列化 保存对象 java反序列化
所有需要保存在磁盘中的对象的类都必须是可序列化的,比如Web应用中需要保存到HttpSession或SevletContext属性中的应用。
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.io.Serializable; public class SerializeTest { public static void main(String[] args) { try { ObjectOutputStream oos=new ObjectOutputStream( new FileOutputStream("src/com/t/color.txt")); Color color=new Color("Red"); oos.writeObject(color);//将对象写入输出流 } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } try { ObjectInputStream ois=new ObjectInputStream( new FileInputStream("src/com/t/color.txt")); //从输入流中读取对象并强制转换成Color类 Color col=(Color)ois.readObject(); System.out.println(col.getColor()); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } } } class Color implements Serializable{ private static final long serialVersionUID = -3398911857061187891L; private String color; public Color(String color) { this.color=color; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } }
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.io.Serializable; public class SerializeTest { public static void main(String[] args) { try { ObjectOutputStream oos=new ObjectOutputStream( new FileOutputStream("src/com/t/color.txt")); Color color=new Color("Red"); Person person=new Person("lavor_zl", color); oos.writeObject(color);//将对象写入输出流 oos.writeObject(person);//将对象写入输出流 } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } try { ObjectInputStream ois=new ObjectInputStream( new FileInputStream("src/com/t/color.txt")); //从输入流中读取对象并强制转换成Color类 Color col=(Color)ois.readObject(); System.out.println(col.getColor()); Person per=(Person)ois.readObject(); System.out.println(per.getName()); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } } } class Color implements Serializable{ /** * */ private static final long serialVersionUID = -3398911857061187891L; private String color; public Color(String color) { this.color=color; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } } class Person implements Serializable{ /** * */ private static final long serialVersionUID = 3426878037487779564L; private String name; private Color color; public Person(String name,Color color) { this.name=name; this.color=color; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Color getColor() { return color; } public void setColor(Color color) { this.color = color; } }
private void writeObject(java.io.ObjectOutputStream out)throws IOException
private void readObject(java.io.ObjectInputStream in)throws IOException,ClassNotFoundException
private void readObjectNoData()throws ObjectStreamException
import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; public class Test implements Serializable{ private static final long serialVersionUID = 6102026641912972144L; private int id; private String name; public Test(int id,String name) { this.id=id; this.name=name; } private void writeObject(ObjectOutputStream out)throws IOException{ out.writeInt(id); out.writeChars(name); } private void readObject(ObjectInputStream in)throws IOException,ClassNotFoundException{ this.id=in.readInt(); //this.name=in.readLine()也可以,不过readLine()已经不推荐使用 this.name=(String) in.readObject(); } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.ObjectStreamException; import java.io.Serializable; import java.util.ArrayList; public class Test implements Serializable{ private int id; private String name; public Test(int id,String name) { this.id=id; this.name=name; } //重写writeReplace()方法,程序在序列化该类对象之前调用该方法 private Object writeReplace()throws ObjectStreamException{ ArrayList<Object> list=new ArrayList<Object>(); list.add(id); list.add(name); return list; } public static void main(String[] args) { try{ ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream("replace.txt")); ObjectInputStream ois=new ObjectInputStream(new FileInputStream("replace.txt")); Test test=new Test(1, "lavor_zl"); oos.writeObject(test); ArrayList<Object> list=(ArrayList<Object>) ois.readObject(); System.out.println(list);//输出:[1, lavor_zl] }catch(Exception e){ e.printStackTrace(); } } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
标签:java序列化 序列化 反序列化 保存对象 java反序列化
原文地址:http://blog.csdn.net/lavor_zl/article/details/42423183