一、序列化
学生类Student
需要实现Serializable接口,否则运行会报错,提示Student无法被序列化
package serializable_32;
import java.io.Serializable;
public class Student implements Serializable{
private int age;
private String name;
private String sex;
private String password;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Student(int age, String name, String sex, String password) {
super();
this.age = age;
this.name = name;
this.sex = sex;
this.password = password;
}
public Student() {
super();
// TODO Auto-generated constructor stub
}
}
序列化Student的对象
package serializable_32;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
public class SeriaStu {
public static void main(String[] args) throws FileNotFoundException {
// TODO Auto-generated method stub
Student student=new Student(18, "安娜", "女", "123456");
//对象输出流
ObjectOutputStream oos=null;
FileOutputStream fos=null;
try {
fos=new FileOutputStream("serializable.txt");
oos=new ObjectOutputStream(fos);
//实例对象序列化
oos.writeObject(student);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
oos.close();
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
运行结果
二、反序列化
还是使用原来的Student类,修改SeriaStu类如下,对student进行序列化和反序列化操作
package serializable_32;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
public class SeriaStu {
public static void main(String[] args) throws FileNotFoundException {
// TODO Auto-generated method stub
Student student=new Student(18, "安娜", "女", "123456");
//对象输出流
ObjectOutputStream oos=null;
FileOutputStream fos=null;
//对象输入流
ObjectInputStream ois=null;
FileInputStream fis=null;
try {
//构建对象输出流,为序列化做准备
fos=new FileOutputStream("serializable.txt");
oos=new ObjectOutputStream(fos);
//构建对象输入流, 为反序列化做准备
fis=new FileInputStream("serializable.txt");
ois=new ObjectInputStream(fis);
//实例对象序列化
oos.writeObject(student);
//实现对象反序列化
Student student2=(Student)ois.readObject();
System.out.println("反序列化的学生信息:"+student2.getName()+"-"+student2.getAge()+
"-"+student2.getSex()+"-"+student2.getPassword());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
ois.close();
fis.close();
oos.close();
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
运行结果
三、避免序列化和反序列化
1.使用transient
修饰Student中的属性,使其避免被序列化和反序列化
//避免密码字段被序列化和反序列化
private transient String password;
2.运行结果如下