对象序列化介绍
对象序列化流: ObjectOutputStream
构造方法
方法名 | 说明 |
---|---|
ObjectOutputStream(OutputStream out) | 创建一个写入指定的OutputStream的ObjectOutputStream |
序列化对象的方法
方法名 | 说明 |
---|---|
void writeObject(Object obj) | 将指定的对象写入ObjectOutputStream |
示例代码
学生类
xxxxxxxxxx
public class Student implements Serializable {
private String name;
private int age;
public Student() {
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
测试类
xxxxxxxxxx
public class ObjectOutputStreamDemo {
public static void main(String[] args) throws IOException {
//ObjectOutputStream(OutputStream out):创建一个写入指定的OutputStream的ObjectOutputStream
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("myOtherStream\\oos.txt"));
//创建对象
Student s = new Student("佟丽娅",30);
//void writeObject(Object obj):将指定的对象写入ObjectOutputStream
oos.writeObject(s);
//释放资源
oos.close();
}
}
注意事项
对象反序列化流: ObjectInputStream
构造方法
方法名 | 说明 |
---|---|
ObjectInputStream(InputStream in) | 创建从指定的InputStream读取的ObjectInputStream |
反序列化对象的方法
方法名 | 说明 |
---|---|
Object readObject() | 从ObjectInputStream读取一个对象 |
示例代码
xxxxxxxxxx
public class ObjectInputStreamDemo {
public static void main(String[] args) throws IOException, ClassNotFoundException {
//ObjectInputStream(InputStream in):创建从指定的InputStream读取的ObjectInputStream
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("myOtherStream\\oos.txt"));
//Object readObject():从ObjectInputStream读取一个对象
Object obj = ois.readObject();
Student s = (Student) obj;
System.out.println(s.getName() + "," + s.getAge());
ois.close();
}
}
serialVersionUID
用对象序列化流序列化了一个对象后,假如我们修改了对象所属的类文件,读取数据会不会出问题呢?
如果出问题了,如何解决呢?
重新序列化
给对象所属的类加一个serialVersionUID
transient
如果一个对象中的某个成员变量的值不想被序列化,又该如何实现呢?
示例代码
学生类
xxxxxxxxxx
public class Student implements Serializable {
private static final long serialVersionUID = 42L;
private String name;
// private int age;
private transient int age;
public Student() {
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
// @Override
// public String toString() {
// return "Student{" +
// "name='" + name + '\'' +
// ", age=" + age +
// '}';
// }
}
测试类
xxxxxxxxxx
public class ObjectStreamDemo {
public static void main(String[] args) throws IOException, ClassNotFoundException {
// write();
read();
}
//反序列化
private static void read() throws IOException, ClassNotFoundException {
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("myOtherStream\\oos.txt"));
Object obj = ois.readObject();
Student s = (Student) obj;
System.out.println(s.getName() + "," + s.getAge());
ois.close();
}
//序列化
private static void write() throws IOException {
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("myOtherStream\\oos.txt"));
Student s = new Student("佟丽娅", 30);
oos.writeObject(s);
oos.close();
}
}
案例需求
创建多个学生类对象写到文件中,再次读取到内存中
实现步骤
代码实现
学生类
xxxxxxxxxx
public class Student implements Serializable{
private static final long serialVersionUID = 2L;
private String name;
private int age;
public Student() {
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
测试类
xxxxxxxxxx
public class Demo03 {
/**
* read():
* 读取到文件末尾返回值是 -1
* readLine():
* 读取到文件的末尾返回值 null
* readObject():
* 读取到文件的末尾 直接抛出异常
* 如果要序列化的对象有多个,不建议直接将多个对象序列化到文件中,因为反序列化时容易出异常
* 建议: 将要序列化的多个对象存储到集合中,然后将集合序列化到文件中
*/
public static void main(String[] args) throws Exception {
/*// 序列化
//1.创建序列化流对象
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("myCode\\oos.txt"));
ArrayList<Student> arrayList = new ArrayList<>();
//2.创建多个学生对象
Student s = new Student("佟丽娅",30);
Student s01 = new Student("佟丽娅",30);
//3.将学生对象添加到集合中
arrayList.add(s);
arrayList.add(s01);
//4.将集合对象序列化到文件中
oos.writeObject(arrayList);
oos.close();*/
// 反序列化
//5.创建反序列化流对象
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("myCode\\oos.txt"));
//6.将文件中的对象数据,读取到内存中
Object obj = ois.readObject();
ArrayList<Student> arrayList = (ArrayList<Student>)obj;
ois.close();
for (Student s : arrayList) {
System.out.println(s.getName() + "," + s.getAge());
}
}
}