
【友情链接】???IO流 之 File类 & 递归 【友情链接】???IO流 之 过滤器 & 字节流 【友情链接】???IO流 之字符流 & 属性集(Properties集合) 【友情链接】???IO流 之 缓冲流 & 转换流 【友情链接】???IO流 之 序列化流 & 打印流
一、序列化流
????????Java提供了一种对象序列化的机制。用一个字节序列可以表示一个对象,该字节序列包含该对象的数据、对象的类型和对象中存储的属性等信息。字节序列写出到文件之后,相当于文件中持久保存了一个对象的信息。 反之,该字节序列还可以从文件中读取回来,重构对象,对它进行反序列化。对象的数据,对像的类型和对像中存储的数据信息,都可以用来在内存中创建对象。 
对象的序列化流,ObjectOutputStream
java.io包下的ObjectOutputStream类继承了OutputStream类,
ObjectOutputStream类的构造方法;
ObjectoutputStream(OutputStream out)
创建写入指定的OutputStream的ObjectOutputStream类的对象。
构造方法中的参数;
OutputStream out:为字节输出流
ObjectOutputStream类的特有的成员方法:
void writeobject(Object obj)
将指定的对象写入到ObjectOutputStream类的对象中。
ObjectOutputStream类的使用步骤:
- 1.创建ObjectOutputStream类的对象,并在构造方法中传递字节输出流
- 2.使用ObjectOutputStream类的对象中的writeobject()方法,将对象写入到文件中
- 3.调用close()方法,释放资渴
示例代码:
import java.io.Serializable;
public class Student implements Serializable {
private String name;
private int age;
private int sid;
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
", sid=" + sid +
'}';
}
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 int getSid() {
return sid;
}
public void setSid(int sid) {
this.sid = sid;
}
public Student() {
}
public Student(String name, int age, int sid) {
this.name = name;
this.age = age;
this.sid = sid;
}
}
public static void main(String[] args) throws IOException {
ObjectOutputStream oos = new ObjectOutputStream(
new FileOutputStream("D:\\BigData\\java\\javalearn\\src\\com\\data\\student.txt"));
Student student = new Student("北慕辰", 18, 18425658);
oos.writeObject(student);
oos.close();
}
测试结果: 
对象的反序列化流,ObjectInputStream
java.io包下的ObjectInputStream类继承了OutputStream类, 该类的作用就是,把文件中保存的对象,以流的方式读取出来使用
1.ObjectInputStream类的构造方法:
ObjectInputStream(InputStream in)
创建从指定Inputstream读取的ObjectInputStream流的对象
参数:
InputStream in:字节输入流
2.特有的成员方法:
Object readobject()
从ObjectInputstream类的对象中,读取对象。
注意:
1.反序列化的前提:
1.类必须实现serializable接口
2.必须存在类对应的cLass文件
2.readObject方法
声明抛出了cLossNotFoundException异常(class文件找不到异常)
当不存在对象的class交件时会抛出此异常
ObjectInputStream类的构使用步逶:
- 1.创建ObjectInputStream类的对象.,并在构造方法中传递字节输入流
- 2.使用ObjectInputStream类的对]象中的readobject()方法,来读取保存对象的文件
- 3.调用close()方法。释放资源
- 4.使用读取出来的对象(打印)
示例代码;
public static void main(String[] args) throws IOException, ClassNotFoundException {
ObjectInputStream ois = new ObjectInputStream(
new FileInputStream("D:\\BigData\\java\\javalearn\\src\\com\\data\\student.txt"));
Object o = ois.readObject();
ois.close();
System.out.println(o.toString());
}
测试结果; 
translent关键字
????????translent关键字为瞬态关键字,是指当一个类中的成员变量,使用了translent修饰后,就不能被序列化。例如private translent age;
序列版本号
????????当JVM反序列化对象时,能够找到class文件,但是class文件在序列化对象之后发生了修改,那么反序列化操作也会失败,并会抛出一个Invalidc1assException 异常。 发生这个异常的原因有如下几种:
- 该类的序列版本号与从流中读取的类描述符的版本号不匹配
- 该类包含未知数据类型
- 该类没有可访问的无参数构造方法
Serializable 接口给需要序列化的类,提供了一个序列版本号。serialVersionUID 该版本号的目的是用于验证序列化的对象和对应类是否版本匹配。 ????????序列化运行时使用一个称为serialVersionUID 的版本号与每个可序列化类相关联,该序列号在反序列化过程中用于验证序列化对象的发送者和接收者是否为该对象加载了与序列化兼容的类。如果接收者加载的该对象的类的 serialVersionUID 与对应的发送者的类的版本号不同,则反序列化将会导致IrvalidClassException 。可序列化类可以通过声明名为serialVersionUID 的字段 (该字段必须是静态(static )、最终(final ) 的 long 型字段)显式声明其自己的serialVersionUID )
private static final long serialVersionUID=1L;
案例:将多个对象存储在集合中,将集合序列化和反序列化。
案例分析,步骤:
1.定义一个存储Student类的对象ArrayList集合
2.向ArrayList集合中存储Student类的对象
3.创建一个序列化流ObjectOutputStream类的对象
4.使用ObjectOutputStream类的对象中的writeObject()方法,对集合进行序列化
5.创建一个反序列化流ObjectInputStream类的对象
6.使用ObjectInputStream类的对象中的readObject()方法读取文件中保存的集合
7.把Object类型的集含转换为ArrayList类型的集合
8.遍历ArrayList集合
9.调用close()方法,释放资源
*/
示例代码:
public static void main(String[] args) throws IOException, ClassNotFoundException {
ArrayList<Student> studentArrayList = new ArrayList<>();
Student student1 = new Student("狗焕", 18, 19045673);
Student student2 = new Student("宝拉", 20, 18045823);
Student student3 = new Student("德善", 17, 19045674);
studentArrayList.add(student1);
studentArrayList.add(student2);
studentArrayList.add(student3);
ObjectOutputStream oos = new ObjectOutputStream(
new FileOutputStream("D:\\BigData\\java\\javalearn\\src\\com\\data\\student.txt"));
oos.writeObject(studentArrayList);
ObjectInputStream ois = new ObjectInputStream(
new FileInputStream("D:\\BigData\\java\\javalearn\\src\\com\\data\\student.txt"));
Object readObject = ois.readObject();
ArrayList<Student> students = (ArrayList<Student>) readObject;
for (Student student : students) {
System.out.println(student);
}
oos.close();
ois.close();
}
测试结果: 
二、打印流:PrintStream
PrintStream打印流,为其他输出流添加了功能,使它们能够方便地打印各种数据值表示形式。 PrintStream特点:
- 1.只负责数据的盘出,不负责数据的读取
- 2,与其他输出流不同,PrintStreom永远不会黏出。IOException
- 3.特有的方法, print , println
void print(任意类型的值)
void println(任意类型的值并换行)
1.打印流的构造方法:
Printstrecm(File fiLe): 输出的目的地是一个文件
PrintStreon(OutputStream out): 输出的目的地是一个字节输出流
PrintStreom(String fileName): 揄出的目的地是一个文件路径
Printstrecm ectends OutputStream
2.打印流继承自父类的成员方法;
public void close():关闭此输出流并释放与此流相关联的任何系统资源。
public void flush():刷新此输出流并强制任何缓冲的输出字节被写出。
public void write(byte[] b)
将b.length字节从指定的字节数组写入此输出流。
public void write(byte[ ] b, int off, int len):
从指定的字节数组写入len字节,从偏移量off开始输出到此输出流
public abstract void write (int b) :将指定的字节输出流。
注意:
如果使用继承自父类的write()方法写数据,那么查看数据的时候会查询编码表
如果使用自己的特有的方法print()/println()方法写数据,则写的数据原样输出
示例代码; 
public static void main(String[] args)throws IOException {
PrintStream ps = new PrintStream("D:\\BigData\\java\\javalearn\\src\\com\\data\\print.txt");
ps.write(97);
ps.println(97);
ps.println("hello");
ps.println('a');
ps.close();
}
测试结果; 
制作不易,各位友友们,大佬们给点鼓励!
点赞👍 👍 👍 收藏+关注 一键三连走起!
|