序列化
ObjectOutStream 对象序列化流 把对象以流的方式写入到文件中保存
构造方法ObjectOutputStream(OutputStream out)
OutputStream out :自己输出流
特有成员方法:writeObjcet(Objcet obj)将指定对象写入ObjcetOutputStream
步骤:
- 创建ObjcetOutputStream对象构造方法中传递字节输出流
- 使用writeObjcet把对象写入文件
- 释放资源
注意:对象必须实现Serialzable接口 不然会报错
Serialzable是一个标记性接口 启动序列化
ObjectInputStream(InputStream in)创建从指定InputStream读取的ObjcetInputStream
参数 InputStream in 字节输出流
特有成员方法
Objcet readObjcet ()从ObjectInputStream读取对象
步骤 - 创建ObjectInputStream对象构造方法中传递字节输入流
- 使用ReadObjcet读取保存对象的文件
- 释放
- 打印
注意:类必须实现SeriaLizable
必须存在类对象的class文件
package Demo69;
import java.io.Serializable;
public class Persin implements Serializable {
String name;
int age;
@Override
public String toString() {
return "Persin{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
public Persin() {
}
public Persin(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;
}
}
package Demo69;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
public class Demo69Object {
public static void main(String[] args) throws IOException {
ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream("g:\\person.txt"));
objectOutputStream.writeObject(new Persin("大美女",18));
objectOutputStream.close();
}
}
package Demo69;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
public class Demo692ObjectReade {
public static void main(String[] args) throws IOException, ClassNotFoundException {
ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream("g:\\Person.txt"));
Object o = objectInputStream.readObject();
objectInputStream.close();
System.out.println(o);
}
}
transient瞬态关键字 也不可以被序列化 不想某个成员变量被序列化 就可以加上这样一个修饰符
static 静态关键字 不可以被序列化
练习 序列化集合 想保存多个对象 把多个对象存储到一个集合中
分析
- 定义一个存储Person对象的ArrayList集合
- 往Arraylist集合中add person对象
- 创建一个序列化流ObjectOutputStream对象
- WriterObjcet对集合进行序列化
- 创建一个反序列化ObjectInputStream对象
- readobject读取文件中保存的集合
- 把Object类型的集合转换为ArrayList类型
- 遍历
- 释放
package Demo69;
import java.io.*;
import java.util.ArrayList;
public class Demo693 {
public static void main(String[] args) throws IOException, ClassNotFoundException {
ArrayList<Persin> arrayList=new ArrayList<>();
arrayList.add(new Persin("zqh",18));
arrayList.add(new Persin("wxy",18));
arrayList.add(new Persin("caohe",18));
arrayList.add(new Persin("liuyu",18));
ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream("g:\\personlist.txt"));
objectOutputStream.writeObject(arrayList);
ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream("g:\\personlist.txt"));
Object o = objectInputStream.readObject();
ArrayList<Persin> list= (ArrayList<Persin>)o;
for(Persin P :list){//遍历输出
System.out.println(P);
}
objectOutputStream.close();
objectOutputStream.close();
}
}
运行结果就是生成一个文件 然后就是文件保存的是乱码