对象的序列化:是指把对象写到一个输出流中
对象的反序列:是指从一个从一个输入流中读取一个对象。
java语言要求只有实现了java.io.serialable接口的类才能序列化和反序列化
对象的序列化包括一下步骤:
(1)创建一个对象输出流
ObjectOutputStream out = new ObjectOutputStream();
new File OutputStream("D:\\objectFile.obj");
(2)通过对象输出流writeObject()方法写对象
out.writeObject("helllo");
out.writeObject(new Date());
对象的反序列化包括一下步骤
(1)创建一个对象输入流
ObjectInputStream input = new ObjectInputStream(new FileInputStream("D:\\obectFile.obj"));
(2)通过对象输入流readObject()方法读取对象
String obj1 = inputStream .readObject();
Date obj2 = out.readObject();
为了能够读出正确的数据,必须保证输出流写入对象和输入流读取对象的顺序一致。
=====================================================================
public class ObjectSaver {
/**
* @param args
* @throws IOException
* @throws FileNotFoundException
* @throws ClassNotFoundException
*/
public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException {
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("F:\\objectFile.obj"));
String obj1 = "hello";
Date obj2 = new Date();
Customer obj3 = new Customer("姚一平", 25);
//序列化对象
out.writeObject(obj1);
out.writeObject(obj2);
out.writeObject(obj3);
out.close();
//反序列对象
ObjectInputStream in = new ObjectInputStream(new FileInputStream("F:\\objectFile.obj"));
String obj11 = (String) in.readObject();
System.out.println("obj11:"+obj11);
System.out.println("obj11 == obj1 "+(obj1==obj11));
System.out.println("obj1.equal(obj1)"+obj1.equals(obj1));
Date obj22 = (Date) in.readObject();
System.out.println("obj22:"+obj22);
System.out.println("obj22 == obj2 "+(obj22==obj2));
System.out.println("obj22.equal(obj2)"+obj22.equals(obj2));
Customer obj33 = (Customer) in.readObject();
System.out.println("obj33:"+obj33);
System.out.println("obj33 == obj3 "+(obj33==obj3));
System.out.println("obj33.equal(obj3)"+obj33.equals(obj3));
}
}
public class Customer { //implements某个东东
private String name ;
private int age;
public Customer(String name,int age) {
this.name = name;
this.age = age;
System.out.println("call second constructor");
}
public boolean equals(Object o){
if(this == o) return true;
if(!(o instanceof Customer)) return false;
final Customer other = (Customer) o;
if(this.name.equals(other.name)&&this.age==other.age)
return true;
else
return false;
}
public String toString(){return "name="+name+",age="+age;};
}
==================================================================================================================================
public class User {//implements这里要实现某个东东
private String name;
private transient String password;//transient
public User(String name, String password) {
super();
this.name = name;
this.password = password;
}
public String toString(){
return name +" "+password;
}
public static void main(String[] args) throws IOException, ClassNotFoundException {
User user = new User("邓宏伟","5188");
System.out.println("Before Serialization:"+user);
ByteArrayOutputStream buf = new ByteArrayOutputStream();
//把user对象序列化到一个字节缓冲容器当中去
ObjectOutputStream o = new ObjectOutputStream(buf);
o.writeObject(user);
//从字节缓存中反序列化User对象
ObjectInputStream in =
new ObjectInputStream(new ByteArrayInputStream(buf.toByteArray()));
user = (User) in.readObject();
System.out.println("After Serialization: "+user);
}
}