对象序列化
定义:通过使用ObjectInputStream和ObjectOutputStream类保存和对象读取的机制叫做序列化机制
简单说就是变成本地文件,导弹来了都不怕,硬盘不坏 数据不丢
ObjectInputStream:对象输入流
ObjectOutputStream:对象输出流
假如现在有一student信息项目,(代码如下)
student.java类
public class Student {
/**
*
*/
//封装
private int stu_id;
private String stu_name;
private int age;
private String school;
//自动生成get和set方法
public int getStu_id() {
return stu_id;
}
public void setStu_id(int stu_id) {
this.stu_id = stu_id;
}
public String getStu_name() {
return stu_name;
}
public void setStu_name(String stu_name) {
this.stu_name = stu_name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSchool() {
return school;
}
public void setSchool(String school) {
this.school = school;
}
//将地址 ----可视化的string
@Override
public String toString() {
return "Student [stu_id=" + stu_id + ", stu_name=" + stu_name + ", age=" + age + ", school=" + school + "]";
}
}
ObjcetStudy类;
public static void main(String[] args) {
Student s =new Student();
s.setStu_id(1000);
s.setStu_name("张三");
s.setAge(23);
s.setSchool("北大");
为了防止执行崩溃,丢失数据,现在将它序列化
先将对象实例化,添加文件流、对象流,并将文件放在一指定磁盘中
所以修改ObjcetStudy类中代码
public static void main(String[] args) {
Student s =new Student();
s.setStu_id(1000);
s.setStu_name("张三");
s.setAge(23);
s.setSchool("北大");
//对象实例化
//先文件输出流
FileOutputStream fos=null;
//对象输出流
ObjectOutputStream oos =null;
//实例化低级流的对象
try {
fos=new FileOutputStream("E:/zhangsan.bin");//要放在本地磁盘中的路径,自己定义
oos= new ObjectOutputStream(fos);
//写出对象
oos.writeObject(s);
System.out.println("执行完毕");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
oos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
修改完ObjcetStudy类此时 还无法执行 代码,
需要在Student类中 添加Serializable 标识接口
该标识此类对象支持序列化
public class Student implements Serializable{
此时需要自动添加一ID
鼠标放置在黄线处,单击添加 ID
此时代码变为:
Student 类:
public class Student implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
//封装
private int stu_id;
private String stu_name;
private int age;
private String school;
//自动生成get和set方法
public int getStu_id() {
return stu_id;
}
public void setStu_id(int stu_id) {
this.stu_id = stu_id;
}
public String getStu_name() {
return stu_name;
}
public void setStu_name(String stu_name) {
this.stu_name = stu_name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSchool() {
return school;
}
public void setSchool(String school) {
this.school = school;
}
//将地址 ----可视化的string
@Override
public String toString() {
return "Student [stu_id=" + stu_id + ", stu_name=" + stu_name + ", age=" + age + ", school=" + school + "]";
}
ObjcetStudy 类
public class ObjcetStudy {
public static void main(String[] args) {
Student s =new Student();
s.setStu_id(1000);
s.setStu_name("张三");
s.setAge(23);
s.setSchool("北大");
//对象实例化
//先文件输出流
FileOutputStream fos=null;
//对象输出流
ObjectOutputStream oos =null;
//实例化低级流的对象
try {
fos=new FileOutputStream("E:/zhangsan.bin");//要放在本地磁盘中的路径,自己定义
oos= new ObjectOutputStream(fos);
//写出对象
oos.writeObject(s);
System.out.println("执行完毕");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
oos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
执行代码,去fos=new FileOutputStream("E:/zhangsan.bin");中自己定义的文件存放路径中查看自己的文件
反序列化
将文件读取到java程序中
//文件输入流
FileInputStream fis=null;
//对象输入流
ObjectInputStream ois=null;
try {
fis=new FileInputStream("E:/zhangsan.bin");
ois=new ObjectInputStream(fis);
//输入对象
Student ss=(Student)ois.readObject();
System.out.println(ss);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
ois.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
执行输出: