为什么从文件中读取对象总是有问题呢?帮帮忙~~

iceandfire 2003-05-27 11:02:40
对象是这样的:
public class InitRecord implements java.io.Serializable
{
private Hashtable column_value;
private Vector column_name;

public InitRecord(Hashtable column_value, Vector column_name)
{
this.column_value = column_value;
this.column_name = column_name;
}
}

如果写一个,读一个,没有问题,可如果写若干个,再读的话就出现异常了,异常如下:
java.io.StreamCorruptedException
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1301)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:324)
at File.readRecord.read(readRecord.java:26)
at File.test.main(test.java:16)

这是从文件中读对象的操作:
package File;

import java.io.*;

/**
* Created by IntelliJ IDEA.
* User: DeceitLei
* Date: 2003-5-27
* Time: 9:49:03
* To change this template use Options | File Templates.
*/
public class readRecord
{
public void read()
{
File theFile = new File("records.bin");
ObjectInputStream objectIn = null;
int objectCount = 0;
InitRecord object = null;

try
{
objectIn = new ObjectInputStream(new FileInputStream(theFile));
while (true)
{
object = (InitRecord)objectIn.readObject();
objectCount++;
}
}
catch (ClassNotFoundException cnfe)
{
cnfe.printStackTrace();
}
catch (EOFException e)
{
System.out.println("EOF readched. " + objectCount + " objects read.");
}
catch (IOException e)
{
e.printStackTrace();
}

try
{
objectIn.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
...全文
34 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
helpall 2003-05-28
  • 打赏
  • 举报
回复
你这样可能不行,需要将readObject(...), writeObject(...)改写.
===========>
不好意思,在这种情况下,你不需要改写,因为Hashtable和Vector都是Serializable的.

但如果你有不能Serializable的数据,你就需要自己写了.
helpall 2003-05-28
  • 打赏
  • 举报
回复
你的writeRecord就是只写一个啊.
helpall 2003-05-28
  • 打赏
  • 举报
回复
static void write(OutputStream out, Vector objects) throws Exception {
for(int i = 0; i < objects...; i++) {
out.writeObject(objects.elementAt(i));
}
}
iceandfire 2003-05-28
  • 打赏
  • 举报
回复
难道我写一个再写一个用FileOutputStream(..,true)的方式避免擦掉以前的文件的方法不行吗?写能写进去,可读的时候就出问题了
helpall 2003-05-27
  • 打赏
  • 举报
回复
你这样可能不行,需要将readObject(...), writeObject(...)改写.
iceandfire 2003-05-27
  • 打赏
  • 举报
回复
这是写的代码,如果把FileOutputStream(....)变为FileOutputStream(..., true)就会产生上面读的错误,这是怎回事呢?难道对象只能在流不关闭的情况下一次写如吗?
package File;

import java.io.ObjectOutputStream;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;


/**
* Created by IntelliJ IDEA.
* User: DeceitLei
* Date: 2003-5-27
* Time: 9:43:35
* To change this template use Options | File Templates.
*/
public class writeRecord
{
public ObjectOutputStream write(InitRecord record)
{
ObjectOutputStream objectOut = null;

try
{
objectOut = new ObjectOutputStream(
new BufferedOutputStream(
new FileOutputStream("records.bin")));
objectOut.writeObject(record);
}
catch (IOException e)
{
e.printStackTrace();
System.exit(1);
}

return objectOut;
}
}
helpall 2003-05-27
  • 打赏
  • 举报
回复
那为什么如果只写一个对象=====>代码呢?
iceandfire 2003-05-27
  • 打赏
  • 举报
回复
有人给解释解释吗?
iceandfire 2003-05-27
  • 打赏
  • 举报
回复
那为什么如果只写一个对象,在用readObject()来读就可以呢?
helpall 2003-05-27
  • 打赏
  • 举报
回复
readObject(...), writeObject(...)是告诉系统怎么去读写一个InitRecord, 不能等同于stream的同名方法.
iceandfire 2003-05-27
  • 打赏
  • 举报
回复
readObject()不就可以从文件中读取一个对象,并返回一个OBJECT引用吗?

62,614

社区成员

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

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