对象序列化就是把一个对象变为二进制的数据量的一种方法,通过对象序列化可以方便地实现对象的传输或存储.
class Person2 implements Serializable {
private String name; // 声明name属性
private int age; // 声明age属性
public Person2(String name, int age) { // 通过构造设置内容
this.name = name;
this.age = age;
}
public String toString() { // 覆写toString()方法
return "姓名:" + this.name + ";年龄:" + this.age;
}
};
public class DemoDesign{
public static void main(String args[]) throws Exception {
File f = new File("D:" + File.separator + "test01.txt") ; // 定义保存路径
ObjectOutputStream oos = null ; // 声明对象输出流
OutputStream out = new FileOutputStream(f) ; // 文件输出流
oos = new ObjectOutputStream(out) ;
oos.writeObject(new Person2("张三",30)) ; // 保存对象
oos.close() ; // 关闭
ObjectInputStream ois = null ; // 声明对象输入流
InputStream input = new FileInputStream(f) ; // 文件输入流
ois = new ObjectInputStream(input) ; // 实例化对象输入流
Object obj = ois.readObject() ; // 读取对象
ois.close() ; // 关闭
System.out.println(obj) ;
}
};
被Serializable接口声明的类的对象的内容都将被序列化,如果现在用户希望自己指定序列化的内容,则可以让一个类实现Externalizeable接口.
但这样做比较麻烦,所以可以在使用Serializable接口实现序列化操作时,对对象中的某个不希望被序列化的属性使用transient关键字进行声明.
public class Person implements Serializable{
private transient String name ; // 声明name属性,但是此属性不被序列化
private int age ; // 声明age属性
public Person(String name,int age){ // 通过构造设置内容
this.name = name ;
this.age = age ;
}
public String toString(){ // 覆写toString()方法
return "姓名:" + this.name + ";年龄:" + this.age ;
}
};<strong>
</strong>
public class SerDemo01{
public static void main(String args[]) throws Exception{
ser() ;
dser() ;
}
public static void ser() throws Exception {
File f = new File("D:" + File.separator + "test.txt") ; // 定义保存路径
ObjectOutputStream oos = null ; // 声明对象输出流
OutputStream out = new FileOutputStream(f) ; // 文件输出流
oos = new ObjectOutputStream(out) ;
oos.writeObject(new Person("张三",30)) ; // 保存对象
oos.close() ; // 关闭
}
public static void dser() throws Exception {
File f = new File("D:" + File.separator + "test.txt") ; // 定义保存路径
ObjectInputStream ois = null ; // 声明对象输入流
InputStream input = new FileInputStream(f) ; // 文件输入流
ois = new ObjectInputStream(input) ; // 实例化对象输入流
Object obj = ois.readObject() ; // 读取对象
ois.close() ; // 关闭
System.out.println(obj) ;
}
};
序列化一组对象:
public class SerDemo02{
public static void main(String args[]) throws Exception{
Person per[] = {new Person("张三",30),new Person("李四",31),
new Person("王五",32)} ;
ser(per) ;
Object o[] = (Object[])dser() ;
for(int i=0;i<o.length;i++){
Person p = (Person)o[i] ;
System.out.println(p) ;
}
}
public static void ser(Object obj[]) throws Exception {
File f = new File("D:" + File.separator + "test.txt") ; // 定义保存路径
ObjectOutputStream oos = null ; // 声明对象输出流
OutputStream out = new FileOutputStream(f) ; // 文件输出流
oos = new ObjectOutputStream(out) ;
oos.writeObject(obj) ; // 保存对象
oos.close() ; // 关闭
}
public static Object[] dser() throws Exception {
File f = new File("D:" + File.separator + "test.txt") ; // 定义保存路径
ObjectInputStream ois = null ; // 声明对象输入流
InputStream input = new FileInputStream(f) ; // 文件输入流
ois = new ObjectInputStream(input) ; // 实例化对象输入流
Object obj[] = (Object[])ois.readObject() ; // 读取对象
ois.close() ; // 关闭
return obj ;
}
};