对象序列化流
package com.czie.iot1913.lps.IO.ObjectStream;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
/**
* FileName: ObjectOutPutStream
* Author: lps
* Date: 2022/3/28 15:07
* Sign:刘品水 Q:1944900433
*/
public class ObjectOutPutStreamDemo {
public static void main(String[] args) throws Exception{
//ObjectOutputStream(OutputStream out)
//创建一个对象写入到指定的输出流。
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("F:\\JavaCode\\oos.txt"));
Student s1 = new Student("刘品水",21);
Student s2 = new Student();
//void writeObject(Object obj)
//写入指定的对象的对象。
oos.writeObject(s1);
//NotSerializableException
// 当一个实例需要实现Serializable接口。序列化运行时或实例类可以抛出此异常。参数应该是类的名称。
//一个类的串行化是由类实现java.io.serializable接口启用。类没有实现这个接口不会有任何序列化或反序列化其状态
oos.close();
}
}
package com.czie.iot1913.lps.IO.ObjectStream;
import java.io.Serializable;
/**
* FileName: Student
* Author: lps
* Date: 2022/3/28 15:12
* Sign:刘品水 Q:1944900433
*/
public class Student implements Serializable {
private String name;
private int age;
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", 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 Student(String name, int age) {
this.name = name;
this.age = age;
}
public Student() {
}
}