对象流:使用流传输对象的过程称为序列化和反序列化
-
序列化:对象存储到存储设备中
-
反序列化:从存储设备中读取出来
-
需要操作文件的流支持
-
不可追加,只能覆盖
package com.li.changGe.ioStream.byteStream.objectStream;
import com.li.changGe.pojo.Student;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
public class ObjectSerializationDemo01 {
private static String fileName = "C:\\Users\\林木\\Desktop/test.txt";
public static void main(String[] args) throws Exception{
objectOutputStreamTest();
objectInputStreamTest();
}
//-------------------------------------------------
public static void objectOutputStreamTest() throws Exception{
Student student = new Student("长歌", '女');
Student student1 = new Student("则天", '女');
Student student2 = new Student("太平", '女');
Student.height = 170.0;
ArrayList<Student> students = new ArrayList<>();
students.add(student);
students.add(student1);
students.add(student2);
//---------------------------------------------------------------
FileOutputStream fileOutputStream = new FileOutputStream(fileName);
//不可追加,只能覆盖
ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream/*,true*/);
//--------------------------------------------------------------------------------------------
/**
* 将对象写入到文件中去
* sr java.util.ArrayListx佉櫱a? I sizexp w sr com.li.changGe.pojo.Studentoj{懀? C sexL changGet Lcom/li/changGe/pojo/ChangGe;L namet Ljava/lang/String;xpYssr com.li.changGe.pojo.ChangGe9d`AJy xpt 闀挎瓕sq ~ Yssq ~ t 鍒欏ぉsq ~ Yssq ~ t 澶钩x
*/
objectOutputStream.writeObject(students);
System.out.println("写入完毕");
objectOutputStream.close();
}
//--------------------------------------------------------
public static void objectInputStreamTest() throws Exception{
FileInputStream fileInputStream = new FileInputStream(fileName);
ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
//只能读取一次
ArrayList<Student> students = (ArrayList<Student>) objectInputStream.readObject();
//报错EOFException
//ArrayList<Student> students1 = (ArrayList<Student>) objectInputStream.readObject();
System.out.println("反序列化完毕");
objectInputStream.close();
//--------------------------------------------------------------------------------------------
Student.height = 40;
/**
* 长歌-女--0.0
* test()方法里调用height:40.0
* I'm LiChangGe.
*/
System.out.println(students.get(0).name+"-"+students.get(0).sex+"-"+"-"+students.get(0).money);
students.get(0).test();
students.get(0).changGe.say();
}
}
对象序列化
-
序列化的对象都需要实现Serializable接口
-
序列化时对象属性也需要实现Serializable接口
-
UID用来在序列化和反序列化时确保对象一致的
-
transient关键字让序列化时屏蔽这个属性
-
transient只能修饰变量
-
静态属性不会被序列化
package com.li.changGe.pojo;
import java.io.Serializable;
/**
* 序列化的对象都需要实现Serializable接口
*/
public class Student implements Serializable {
//UID用来在序列化和反序列化时确保对象一致的
private static final long serialVersionUID = 8028258403768056093L;
public String name;
public char sex;
//transient让序列化时屏蔽这个属性
transient public double money = 5.0;
//静态属性不会被序列化
public static double height = 5.0;
//----------------------------------------------------
/**
* 序列化时对象属性也需要实现Serializable接口
* 不然会报错:NotSerializableException
*/
public ChangGe changGe = new ChangGe();
public Student() {}
public Student(String name, char sex) {
this.name = name;
this.sex = sex;
}
//-------------------------------------------------------
/**
* transient只能修饰变量
* 静态成员都不会被序列化
*/
/**
* 序列化是用来保存和对象有关的信息
*
* 静态常量池的东西在类加载时就已经存在了
* 它是属性类的东西,不属性对象
*/
/*transient */public static void test(){
System.out.println("test()方法里调用height:"+height);
}
}
package com.li.changGe.pojo;
import java.io.Serializable;
public class ChangGe implements Serializable {
public void say(){
System.out.println("I'm LiChangGe.");
}
}