使用ObjectInputStream的readObject()方法如何判断读取到多个对象的结尾

摘自http://blog.csdn.net/fjdingsd/article/details/46765803

使用ObjectInputStream的readObject()方法如何判断读取到多个对象的结尾

import java.io.EOFException;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream; import cn.com.mybolg.bean.Person; /*
* 若文件中有若干个Object对象,你用ObjectInputStream中的readObject()去读,何时判读到结尾?
* 方法之一:(常用的方法)将若干个对象(数量不定)都装入一个容器中(如:ArrayList之类),
* 然后将容器这一个对象写入就行了。读取时,只要读取一个对象(即容器对象)就行了。
*
* 方法之二:(若不想用容器),则由于数量不定,正是用EOFException来判断结束。
* 代码结构如下:(无论是readInt()读int,还是readObject()读对象)
* try{
* while(true) {
* Object o=ois.radObject();
* 处理已读出的对象o;
* }
* }
* catch(EOFException e){
* //已从流中读完。
* }
* finallly {
* 流的关闭。 }
*/ public class ObjectInputStreamDemo { public static void main(String[] args) throws IOException, ClassNotFoundException { FileInputStream fis = new FileInputStream("F:\\person.object");
ObjectInputStream osi = new ObjectInputStream(fis); try {
while(true) {
Person p = (Person) osi.readObject();
System.out.println(p.getName()+":"+p.getAge());
}
}
catch(EOFException e) { }
finally {
osi.close();
}
} }

以下是自己的试验:

往文件里存多个对象也没问题,调用writeObject()一直往里写即可,readObject()一次返回一个对象,再readObject()一次返回第二个对象,自动在每个对象后面做标记。并且同一个文件中可以依次存入不同类的对象。先存入一个Person对象,再存入一个Demo对象,再存个Haha对象都可以。按顺序读就行了。

使用ObjectInputStream的readObject()方法如何判断读取到多个对象的结尾

package packa;

import java.io.*;

class ObjectInputOutputStream
{
public static void main(String[] args)throws Exception
{
writeObj();
readObj();
} private static void writeObj()throws Exception
{
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("person.object")); oos.writeObject(new Person("liu", 30, "kr"));
oos.writeObject(new Person2("liu", "kr", "kr2"));
oos.writeObject(new Person3(10, 30, 50)); oos.close();
} private static void readObj()throws Exception
{
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("person.object"));
System.out.println(ois.readObject());
System.out.println(ois.readObject());
System.out.println(ois.readObject()); ois.close();
}
}
package packa;

import java.io.*;

class Person implements Serializable
{
public static final long serialVersionUID = 44L; String name;
transient int age;
static String country = "cn"; Person(String name, int age, String country)
{
this.name = name;
this.age = age;
this.country = country;
} public String toString()
{
return name + " " + age + " " + country;
} }
class Person2 implements Serializable
{
public static final long serialVersionUID = 45L; String name;
String country;
String country2; Person2(String name, String country, String country2)
{
this.name = name;
this.country = country;
this.country2 = country2;
} public String toString()
{
return name + " " + country + " " + country2;
} }
class Person3 implements Serializable
{
public static final long serialVersionUID = 46L; int age;
int age2;
int age3; Person3(int age, int age2, int age3)
{
this.age = age;
this.age2 = age2;
this.age3 = age3;
} public String toString()
{
return age + " " + age2 + " " + age3;
} }
上一篇:BZOJ 1011 [HNOI2008]遥远的行星


下一篇:折腾gcc/g++链接时.o文件及库的顺序问题