ObjectInputStream 遍历读取.tmp中的类对象

坟墓里的爱情 2014-06-16 03:37:30
如果tmp文件中有两个类对象,如何遍历取出。目前可以分两次读出,代码如下
package com.donghe.package1;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;

public class ObjectIntputStreamDemo {
public static void main(String[] args){
ObjectInputStream ois =null;
try {
ois = new ObjectInputStream(new FileInputStream("e:\\person.tmp") );
Object ob = ois.readObject();

System.out.println(ois.read());

if (ob!= null){
Person p= (Person)ob;
System.out.println(p.getId());
System.out.println(p.getName());
System.out.println(p.getAge());
}
System.out.println(ois.read());

Object ob2 = ois.readObject();
if (ob2!= null){
Person p= (Person)ob2;
System.out.println(p.getId());
System.out.println(p.getName());
System.out.println(p.getAge());
}
System.out.println(ois.read());


} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
ois.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
...全文
879 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
坟墓里的爱情 2014-06-20
  • 打赏
  • 举报
回复
把N和Person对象一块放到流中?
dandan8989251 2014-06-20
  • 打赏
  • 举报
回复
楼上.你说的这个方法也是可行的..但要根据需求兼顾性能最简单的方法来做!你的代码都用了两次流了...代码又长又臭..可不是每个人都喜欢的
vnvlyp 2014-06-20
  • 打赏
  • 举报
回复
写Object之前先写入一个int表示有多少个Object,哪里有你想的那么复杂?ObjectInputStream和ObjectOutputStream本来就支持readInt和writerInt,方便得很,看下面的例子。 输出 1 5 20
class Person implements Serializable {
	private static final long serialVersionUID = 2243668234110136779L;
	int age;
	Person(int age) {
		this.age = age;
	}
}

public class Test {
	public static void main(String[] args) throws Exception {
		Person[] persons = new Person[3];
		persons[0] = new Person(1);
		persons[1] = new Person(5);
		persons[2] = new Person(20);
		
		ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("D:\\test"));
		
		oos.writeInt(persons.length);
		for (int i = 0; i < persons.length; i++) {
			oos.writeObject(persons[i]);
		}
		
		oos.close();
		
		ObjectInputStream ois = new ObjectInputStream(new FileInputStream("D:\\test"));
		
		int num = ois.readInt();
		persons = new Person[num];
		for (int i = 0; i < num; i++) {
			persons[i] = (Person)ois.readObject();
		}
		
		ois.close();
		
		for (int i = 0; i < persons.length; i++) {
			System.out.println(persons[i].age);
		}
	}
}
dandan8989251 2014-06-20
  • 打赏
  • 举报
回复
4楼的方法也是可行的....但是太麻烦....代码太多.....不够简单....
dandan8989251 2014-06-20
  • 打赏
  • 举报
回复
方法之一:(常用的方法)将若干个对象(数量不定)都装入一个容器中(如:ArrayList之类),然后将容器这一个对象写入就行了。读取一个对象(即容器对象)就行了。推荐用第一种.省代码 方法之二:(若不想用容器),则由于数量不定,正是用EOFException来判断结束。代码结构如下:(无论是readInt()读int,还是readObject()读对象) try{ while(true) { Object o=ois.radObject(); 处理已读出的对象o; } }catch(EOFxception e){ //已从流中读完。 } finallly{ //流的关闭。 }
shine333 2014-06-16
  • 打赏
  • 举报
回复
不是,拜托! 是N个Person之前,放单独一个int,这个int的数值就是N
坟墓里的爱情 2014-06-16
  • 打赏
  • 举报
回复
你是说一个person对象对应一个数字?这样的话需要改person类了,加上个字段private int index; 对不? 就算这样,从tmp文件往外取对象的时候,也是需要遍历的,readObject方法执行一次只能取得一个对象。
shine333 2014-06-16
  • 打赏
  • 举报
回复
楼主,如果文件是你自己写的,你不会一开始往里面写个int啊,代表接下来,有几个Person
坟墓里的爱情 2014-06-16
  • 打赏
  • 举报
回复
貌似有办法了,用while循环结合异常捕捉机制。
package com.donghe.package1;

import java.io.EOFException;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;

public class ObjectIntputStreamDemo {
		public static void main(String[] args){
			ObjectInputStream ois =null;
			try {
				
				FileInputStream fileStream =new FileInputStream("e:\\person.tmp");
				ois = new ObjectInputStream(fileStream);
				
				while (true) {
					Object ob = ois.readObject();
					if(ob!=null){
						Person p= (Person)ob;
						System.out.println(p.getId());
						System.out.println(p.getName());
						System.out.println(p.getAge());
					}
				}
			} catch(EOFException e){
				System.out.println("类对象已完全读入");
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}finally{
				try {
					ois.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
}
能输出N个类对象的属性
坟墓里的爱情 2014-06-16
  • 打赏
  • 举报
回复
Object obj = ois.readObject();只能读出文件中的一个类对象,但是文件中可以有多个类对象,我想问的是在不确定文件中有多少个对象的时候,怎样遍历取出这多个对象。
艾德 2014-06-16
  • 打赏
  • 举报
回复
没看明白,这样不行吗?

public static Person readPerson(ObjectInputStream ois){
    Object obj = ois.readObject();
    return (Person)obj
}

62,615

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧