写多个可加化物体的对象状态。 [英] write mutiple object states of serilizable object.

查看:44
本文介绍了写多个可加化物体的对象状态。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以在一个文件中写出可序列化的多个对象状态吗?

如果可以的话我怎样才能读回这些状态?


i有在文件中写入多个对象状态没问题。

但是我无法读回所有对象,说明我到目前为止所保存的内容。


only读回第一个状态然后抛出 StreamCorruptedException 异常。


i知道这是由于文件中的数据不一致。


所以PLZ帮帮我。

问候。

can i write multiple object states of a serializable in a single file?
if i can then how can i read back those states?

i have no problem with writing multiple object states in a file.
but i can''t read back the all object states what i saved so far.

only after reading back the first state then StreamCorruptedException exception thrown.

i know it is due to data inconsistency in the file.

so plz help me.
Regards.

推荐答案


我可以写吗单个文件中可序列化的多个对象状态?

如果可以的话我怎样才能读回这些状态?


i写入多个对象没有问题文件中的状态。

但是我无法读回所有对象,说明我到目前为止所保存的内容。


只有在读回第一个州之后然后 StreamCorru ptedException 异常抛出。


i知道这是由于文件中的数据不一致。


所以请帮助我。

问候。
can i write multiple object states of a serializable in a single file?
if i can then how can i read back those states?

i have no problem with writing multiple object states in a file.
but i can''t read back the all object states what i saved so far.

only after reading back the first state then StreamCorruptedException exception thrown.

i know it is due to data inconsistency in the file.

so plz help me.
Regards.



我们可以看到代码。

Can we see the codes.



我可以写多个对象单个文件中可序列化的状态?

如果可以的话我怎样才能读回这些状态?


i在编写多个对象状态时没有问题一个文件。

但是我无法读回所有对象,说明我到目前为止所保存的内容。


只有在读回第一个状态后才< b> StreamCorruptedException 异常抛出。


i知道这是由于文件中的数据不一致。


所以请帮助我。

问候。
can i write multiple object states of a serializable in a single file?
if i can then how can i read back those states?

i have no problem with writing multiple object states in a file.
but i can''t read back the all object states what i saved so far.

only after reading back the first state then StreamCorruptedException exception thrown.

i know it is due to data inconsistency in the file.

so plz help me.
Regards.



除非你重置ObjectOutputStream或writeUnshared(),否则每个对象只会被写入这样一个流的
。以下每次尝试编写对象

时,只会对以前序列化的化身的引用写入流中。


亲切的问候,


Jos

Unless you reset the ObjectOutputStream or writeUnshared() every object is
written to such a stream only once. Every following attempt to write the object
only a reference to a previously serialized incarnation is written to the stream.

kind regards,

Jos


实际上这个Q.是由我代表AKB发送的。所以我发送源代码。


按照 ro35198x 要求的关注...


我的代码是这样的......

actually this Q. is sent by me on behalf of AKB. so i send u the source code.

as per as ro35198x requirements concern ...

my code is something like this .....


//这是我要序列化的SERIALIZABLE类。


class s实现了Serailizable

{

private int a,b;

public s(int A,int B){a = A; b = B;}

public void display(){

System.out.println(" DMJ:" + a +" \t" ; + b);

}

}


//这是我的主要班级


class m

{

public static void main(String args [])

{

FileOutputStream o = new FileOutputStream(file_name,true);

//以追加模式打开的文件

ObjectOutputStream out = new ObjectOutputStream(o);

out.writeObject(new s(100,200) );

out.writeObject(new s(300,400));


FileInputStream i = new FileInputStream(file_name);

ObjectInputStream in = new ObjectInputStream(i);

((s)in.readObject())。display(); //这没关系......它显示我保存的第一个状态。

((s)in.readObject())。display();

//第二个电话是抛出StreamCorruptedException。

}

}
//this is my SERIALIZABLE class which is to be serialized.

class s implements Serailizable
{
private int a ,b;
public s(int A,int B){a=A;b=B;}
public void display(){
System.out.println("DMJ: " + a + "\t" + b);
}
}

//this is my main class

class m
{
public static void main(String args[])
{
FileOutputStream o = new FileOutputStream(file_name,true);
//the file opened in append mode
ObjectOutputStream out = new ObjectOutputStream(o);
out.writeObject(new s(100,200));
out.writeObject(new s(300,400));

FileInputStream i = new FileInputStream(file_name);
ObjectInputStream in = new ObjectInputStream(i);
((s)in.readObject()).display(); //this is OK ... it shows the first state i saved.
((s)in.readObject()).display();
//the second call is throwing StreamCorruptedException.
}
}



plz help。

亲切的问候。

plz help.
kind regards.


这篇关于写多个可加化物体的对象状态。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆