ObjectInputStream ObjectOutputStream 问题求助

lv_teng 2011-02-24 08:15:11


import java.io.*;
public class TestObject {


public TestObject() {
}


public static void main(String[] args) throws FileNotFoundException,IOException,ClassNotFoundException{
try{ Staff a=new Staff();
a.t=2;
Staff b=new Staff();
b.t=3;

FileOutputStream fos=new FileOutputStream("../test.txt",true);
BufferedOutputStream bos=new BufferedOutputStream(fos);
ObjectOutputStream oos=new ObjectOutputStream(bos);
System.out.println("-1");
oos.writeObject(a);
System.out.println(a);
oos.writeObject(b);
System.out.println(b);
FileInputStream fis=new FileInputStream("../test.txt");
System.out.println("5");
BufferedInputStream bis=new BufferedInputStream(fis);
System.out.println("6");
ObjectInputStream ois=new ObjectInputStream(bis);
System.out.println("0");
System.out.println(ois.readObject());
System.out.println("1");
System.out.println(ois.readObject());
System.out.println("2");

oos.flush();
bos.flush();
fos.flush();
oos.close();
bos.close();
fos.close();
fis.close();
bis.close();
ois.close();
}

catch(EOFException sd)
{
System.out.println("Exception");
}


}}
class Staff implements Serializable
{
int t=0;
}
//运行结果:-1
//Staff@1a758cb
//Staff@1b67f74
//5
//6
//Exception
//为什么从0开始后面的都没输出了。是什么问题。我想测试的是作为Object 读出读入文件

...全文
103 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
lv_teng 2011-03-20
  • 打赏
  • 举报
回复
关于ObjectInputStream 只能读出第一条记录的问题。差不多有这样一个说法。是说。一次一次存进去内存会在每个对象中间添加一个标记符。所以读出来的格式就只能读出第一个对象。因为。识别不了中间的分隔符。不过个人认为,既然以这种方式存储。并有相应的读取类。应该是不会有这个问题的。。待有谁知道。发我邮箱吧谢谢。286480625@qq.com
lv_teng 2011-03-04
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 model_cz 的回复:]
恩,肯定是出异常了,把catch(EOFException e)改成catch(Exception e)试试。再就是不要再一次循环里两次调用oi.readObject();还有,最好在读到对象后判断一下对象是否为空,如果不为空再staff.add((Staff)o),否则break;
[/Quote] 不好意思 好久没登。catch(EOFException e)就是用来判断文件是否读完的。不然没有办法判定。
Model_CZ 2011-02-26
  • 打赏
  • 举报
回复
恩,肯定是出异常了,把catch(EOFException e)改成catch(Exception e)试试。再就是不要再一次循环里两次调用oi.readObject();还有,最好在读到对象后判断一下对象是否为空,如果不为空再staff.add((Staff)o),否则break;
lv_teng 2011-02-25
  • 打赏
  • 举报
回复
感谢楼上几位。。说的都对。感谢。 但是我在做一个管理系统。发现不论存了多少条记录进去 都只能读出第一条。 我看了下文本。容量有在增加。就是读只能读第一个,。我发我在读程序的代码,。


package staffInformationManagement;

import java.io.*;
import java.util.*;

class IOTxt implements readWriteinterface{


static ObjectInputStream i=null;
static BufferedInputStream in=null;

private ObjectInputStream readAStaff() throws IOException, ClassNotFoundException
{ FileInputStream omg=new FileInputStream("../records.txt");
in=new BufferedInputStream(omg);
i=new ObjectInputStream(in);
return i;
}
public ArrayList read() throws IOException, ClassNotFoundException
{
ArrayList<Staff> staff=new ArrayList<Staff>();
//System.out.println("1");
Staff astaff=null;
ObjectInputStream oi=null;
oi=readAStaff();
try{
while(true)
{ // System.out.println("1");

// System.out.println("2");
Object o=oi.readObject();
// 处理已读出的对象o;
// System.out.println("3");
staff.add((Staff)o);
//System.out.println(((Staff)o).employeePayrollNumber);
System.out.println("fdsfdsf");//这个有输出
System.out.println((Staff)(oi.readObject()));//这个不会打印。

}
}catch(EOFException e){
System.out.println("5"); //已从流中读完。这个不会运行到。
}
finally{
// 流的关闭。
oi.close();
i.close();
return staff;
}
}

public void write(Staff staff)
{

try{
FileOutputStream fout=new FileOutputStream("../records.txt",true);
BufferedOutputStream out=new BufferedOutputStream(fout);
ObjectOutputStream o=new ObjectOutputStream(out);

o.writeObject(staff);
System.out.println(staff.departmentNumber);
o.flush();
o.close();
System.out.println("\r\n Record Saved in txt");

}

catch(Exception ie)
{
ie.printStackTrace();


}
}


}







Model_CZ 2011-02-25
  • 打赏
  • 举报
回复
指出你程序的亮点错误:
1. 如果没有锁的保护,最好不好同时打开同一个文件的输入流和输出流,即这样的结构是有问题的:
打开输出流>写内容>打开输入流>读内容>关闭输入流>关闭输出流

打开输出流>写内容>打开输入流>读内容>关闭输入流>关闭输出流
正确的代码是:
打开输出流>写内容>flush(可选)>关闭输出流 打开输入流>读内容>关闭输入流

打开输入流>读内容>关闭输入流 打开输出流>写内容>flush(可选)>关闭输出流

2. 对于包装多层流的操作: 无需每层都flush和close,只需flush和关闭最外层的就可以,如:
oos.flush();
bos.flush();
fos.flush();
oos.close();
bos.close();
fos.close();
应改成
oos.flush();
oos.close();
就可以。

针对以上为题,我将你的代码修改如下:

import java.io.*;

public class TestObject {

public TestObject() {
}

public static void main(String[] args) throws FileNotFoundException,
IOException, ClassNotFoundException {
try {
Staff a = new Staff();
a.t = 2;
Staff b = new Staff();
b.t = 3;

FileOutputStream fos = new FileOutputStream("d:/test.txt", true);
BufferedOutputStream bos = new BufferedOutputStream(fos);
ObjectOutputStream oos = new ObjectOutputStream(bos);
System.out.println("-1");
oos.writeObject(a);
System.out.println(a);
oos.writeObject(b);
System.out.println(b);
//flush并关闭输出流:
oos.flush();
oos.close();

FileInputStream fis = new FileInputStream("d:/test.txt");
System.out.println("5");
BufferedInputStream bis = new BufferedInputStream(fis);
System.out.println("6");
ObjectInputStream ois = new ObjectInputStream(bis);
System.out.println("0");
System.out.println(ois.readObject());
System.out.println("1");
System.out.println(ois.readObject());
System.out.println("2");
//flush并关闭输入流:
ois.close();
}

catch (EOFException sd) {
System.out.println("Exception");
}

}
}

class Staff implements Serializable {
int t = 0;
}
Mybeautiful 2011-02-25
  • 打赏
  • 举报
回复
我测试了下,如楼上所说,
oos.flush(); 或者bos.flush(); 一定要放在 5之前;否则 test.txt其实还是空的。
楼主可以调试一下,你会发现 在flush之前, test.txt的大小一直是0。
ee34471 2011-02-24
  • 打赏
  • 举报
回复
将oos.flush();
bos.flush();
fos.flush();
oos.close();
bos.close();
fos.close();放到FileInputStream fis=new FileInputStream("../test.txt")前,且其实直接close掉oos就可以了
lv_teng 2011-02-24
  • 打赏
  • 举报
回复
来个人啊。。我这两天为这个焦虑死了
lv_teng 2011-02-24
  • 打赏
  • 举报
回复
求助啊。为什么没人回帖啊。

62,614

社区成员

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

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