ObjectInputStream:对象输入流
ObjectOutputStream:对象输出流
此流可以往文件中存取对象
案例:
package com.tanghaibin.io;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import org.junit.Test;
public class ObjectInputStreamDemo {
ObjectInputStream ois = null;
ObjectOutputStream oos = null;
String path = "G:"+ File.separator+"io"+File.separator+"object.txt";
@Test
public void objectOutput() throws FileNotFoundException, IOException{
Person person = new Person("simple",14);
oos = new ObjectOutputStream(new FileOutputStream(path));
oos.writeObject(person);
}
@Test
public void objectInput() throws FileNotFoundException, IOException, ClassNotFoundException{
ois = new ObjectInputStream(new FileInputStream(path));
Person person = (Person) ois.readObject();
System.out.println(person.getName()+":"+person.getAge());
}
}
class Person implements Serializable{
private String name;
private transient int age;
public Person(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;
}
}