package IOliu.ObjectStream;
import java.io.*;
/**
* 对象操作流:可以用于读写对象
* ObjectOutStreadm;
* writeObject
* ObjectOutputStream(OutputStream out)
* 创建写入指定 OutputStream 的 ObjectOutputStream。
* ObjectInputStream
* readObject
* ObjectInputStream(InputStream in)
创建从指定 InputStream 读取的 ObjectInputStream。
*
*
* 注意事项:
* 使用对象输出流写出对象,只能使用对象输入流来读取对象
* 只能将支持java.io.Serializable接口的对象写入流中
*
* 使用对象输出流和读对象输入流写对象
* Exception in thread "main" java.io.NotSerializableException: IOliu.ObjectStream.Student
Serializable:序列号,是一个标识接口,只有标识作用,没有方法
* 当一个类的对象需要IO流进行读写的时候,这个类必须实现该接口
*
*
* EOFException 当输入过程中意外到达文件或流的末尾时,抛出此异常
* 如何解决这个异常
* A 捕获异常
* B 对象集合 只读一次
*
*
* */
public class ObjectOutStreadmDemo {
public static void main(String[] args) throws IOException, ClassNotFoundException {
//// ObjectOutStreadm;
// ObjectInputStream
method1();
ObjectInputStream ois=new ObjectInputStream(new FileInputStream("a.txt"));
// Object o1 = ois.readObject();
// System.out.println(o1);
//
// Object o2 = ois.readObject();
// System.out.println(o2);
//
// Object o3 = ois.readObject();
// System.out.println(o3);
try {
while (true){
Object o1 = ois.readObject();
System.out.println(o1);
}
}catch (EOFException e){
System.out.println("文件末尾");
}
ois.close();
}
public static void method1() throws IOException {
//创建对象输出流的对象
// FileInputStream fis=new FileInputStream("F:\\untitledxuexi\\fr.txt");
// ObjectInputStream oss=new ObjectInputStream(fis);
ObjectOutputStream oss=new ObjectOutputStream(new FileOutputStream("a.txt"));
Student t1=new Student("zhangsan",16);
Student t2=new Student("lisi",18);
oss.writeObject(t1);
oss.writeObject(t2);
// oss.writeOb
oss.close();
}
}
删除线格式
package IOliu.ObjectStream;
import java.io.*;
import java.lang.reflect.Array;
import java.util.ArrayList;
/**
*
* EOFException 当输入过程中意外到达文件或流的末尾时,抛出此异常
* 如何解决这个异常
* A 捕获异常
* B 对象集合 只读一次 这个Demo采取这个方法
*
*/
public class ObjectDemo2 {
public static void main(String[] args) throws IOException, ClassNotFoundException {
// method1();
method2();
}
public static void method2() throws IOException, ClassNotFoundException {
ObjectInputStream ois=new ObjectInputStream(new FileInputStream("a.txt"));
Object o = ois.readObject();
System.out.println(o);
//向下还原
ArrayList<Student> list=(ArrayList<Student>) o;
System.out.println(list.get(0));
System.out.println(list);
ois.close();
}
public static void method1() throws IOException {
ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream("a.txt"));
ArrayList<Student> arrayList=new ArrayList <>();
arrayList.add(new Student("wangwu",28));
arrayList.add(new Student("zhaoliu",38));
oos.writeObject(arrayList);
oos.close();
}
}
删除线格式
删除线格式
package IOliu.ObjectStream;
import java.io.Serializable;
/**
* InvalidClassException
* 该类的序列版本号与从流中读取的类描述符的版本号不匹配
该类包含未知数据类型
该类没有可访问的无参数构造方法
* 当我们用对象输入流之后,类会产生一个序列号 文件会记住这个序列号,当我们改变类之后 序列号会发生改变
* 所以会产生这个异常InvalidClassException
* 解决方法: 生成一个序列号
*
* */
public class Student implements Serializable {
// private static final long serialVersionUID = -8761391123951334202L;
String name;
int age;
int g;
// transient int num; transient 可以保护变量不被读取到
public Student(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}