对象流
ObjectInputStream / ObjectOutputStream 要求读写的对象必须进行序列化
ObjectInputStream
ObjectOutputStream
序列化的目的
就为了对象的传输。保证传输对象的一致性。
如何序列化
只需要将需要序列化的类实现 Serializable 接口就可以了
可以使用对象流来读取和写出一个对象或者是基本数据类型的对象(包装类)。
应用
进行对象的持久化
package org.lanqiao.oobjectstream.demo;
import java.io.Serializable;
public class Student implements Serializable{
/**
*
*/
private static final long serialVersionUID = -4537979980241951582L;
private String name;
private int age;
public Student() {
super();
}
public Student(String name, int age) {
super();
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;
}
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + "]";
}
}
package org.lanqiao.oobjectstream.demo;
import java.io.EOFException;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
public class ObjectOutputStreamDemo {
public static void main(String[] args) throws IOException, IOException, ClassNotFoundException {
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("obj.dbf"));
//准备写出的数据
int i= 10;
boolean b = true;
Student stu = new Student("张三",21);
Student stu1 = new Student("李四",23);
/*oos.writeInt(i);
oos.writeBoolean(b);*/
oos.writeObject(stu);
oos.writeObject(stu1);
/*ObjectInputStream ois = new ObjectInputStream(new FileInputStream("obj.dbf"));
int ii = ois.readInt();
System.out.println(ii);
boolean bb = ois.readBoolean();
System.out.println(bb);*/
/*Student stuo = (Student) ois.readObject();
System.out.println(stuo.getName());
System.out.println(stuo.getAge());
Student stuo1 = (Student) ois.readObject();
System.out.println(stuo1.getName());
System.out.println(stuo1.getAge());*/
//再持久化的对象中,查找特定对象
/*Student s = findStu("李四", ois);
System.out.println(s);
System.out.println("-----------------");
release(ois, null);*/
ObjectInputStream ois1 = new ObjectInputStream(new FileInputStream("obj.dbf"));
List<Student> stuList = findAll(ois1);
for(Student stus :stuList) {
System.out.println(stus);
}
release(ois1, oos);
}
public static Student findStu(String name,ObjectInputStream ois) {
try {
Object obj = "";
while((obj = ois.readObject()) != null) {
Student stu0 = (Student) obj;
if(stu0.getName().equals(name)) {
return stu0;
}
}
}catch (EOFException e) {
System.out.println("已经没有可读取的对象");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
public static List<Student > findAll(ObjectInputStream ois) {
List<Student> stuList = new ArrayList<>();
try {
Object obj = "";
while((obj = ois.readObject()) != null) {
Student stu0 = (Student) obj;
stuList.add(stu0);
}
}catch (EOFException e) {
System.out.println("已经没有可读取的对象");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return stuList;
}
public static void release(InputStream in,OutputStream os) {
if(in != null) {
try {
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(os != null) {
try {
os.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}