以前在文件中写入数据的主要方式是用字符流或者字节流。但是如果想保存并读取一个对象,该怎么办?可以是用ObjectOutputStream类 和 ObjectInputStream类。
ObjecOutputStream类
常用构造方法:ObjectOutputStream oos = new ObjectOutputStream(OutputStream out);//创建一个写入指定OutputStream的ObjectOutputStream对象.
如:ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("F:\\obj.object"));
主要方法:void writeObject(Object obj);//将指定的对象写入ObjectOutputStream.对对象进行序列化。
注意:被写入的对象必须实现Serializable接口。
void write(byte[] buf);
void write(byte[] buf,int off,int len);
void write(int val);//写入一个字节。
void writeBoolean(boolean val);
void writeByte(int val);//写入一个八位字节。
void writeBytes(String str);//以字节序列形式写入一个String。
void writeChar(int val);//写入一个16位的char值。
viod writeChars(String str);//以char序列形式写入一个String。
viod writeUTF(String str);//以UTF-8修改版格式写入此String的基本数据。
ObjectInputStream类
常用构造方法:ObjectInputStream ois = new ObjectInputStream(InputStream in);//创建从指定 InputStream 读取的 ObjectInputStream。
如:ObjectInputStream ois = new ObjectInputStream(new FileInputStream("F:\\obj.object"));
主要方法:Object readObject();//从ObjectInputStream读取对象。由于其返回的是Object类型 需要对其进行强转。
int read();//读取单个字节。
int read(byte[] buf,int off,int len);//读入byte数组,返回读取字节的实际个数。
boolean readBoolean();//读取一个boolean值
byte readByte();//读取一个8位的字节。
char readChar();//读取一个16为的char值。
int readInt();读取一个32位的int值。
String readUTF();读取一个UTF-8 修改版格式的String.
代码如下:
person对象类
package ObjectStream;
import java.io.Serializable;
/**
* 继承Serializable接口用于给该标示一个ID号。该id号有虚拟机根据该类运算得出。
*
* 用于判断该类和obj对象文件是否 为同一个版本。当不为同一个版本时将会抛出无效类异常。
* @author Administrator
*
*/
public class Person implements Serializable/*标记接口*/{
/**
* 由于在定义对象时需要定义一个静态final的long字段serialVersionUID。
* 如果不定义的话系统将根据该类各个方面进行计算该值,极容易导致读取异常。
*/
private static final long serialVersionUID = 5201314l;
// static 和 transient修饰符修饰得都不会被写入到对象文件中去。
//非瞬态和非静态字段的值都将被写入
private String name;
private int age;
public Person(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;
}
}
主程序
package ObjectStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
public class ObjectStreamDemo {
/**
* @param args
* @throws IOException
* @throws FileNotFoundException
* @throws ClassNotFoundException
*/
public static void main(String[] args) throws IOException, ClassNotFoundException {
writeMethod();
// readMethod();
}
public static void readMethod() throws IOException, ClassNotFoundException {
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("F:\\obj.object"));
Person p = (Person)ois.readObject();
System.out.println(p.getName()+" : "+p.getAge());
p = (Person)ois.readObject();
System.out.println(p.getName()+" : "+p.getAge());
p = (Person)ois.readObject();
System.out.println(p.getName()+" : "+p.getAge());
p = (Person)ois.readObject();
System.out.println(p.getName()+" : "+p.getAge());
}
public static void writeMethod() throws IOException, FileNotFoundException {
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("F:\\obj.object"));
//对象序列化。 被序列化的对象必须实现Serializable接口。
oos.writeObject(new Person("小强",20));
oos.writeObject(new Person("小明",26));
oos.writeObject(new Person("小李",25));
oos.writeObject(new Person("李煜",19));
oos.close();
}
}