gpt4 book ai didi

java - 从字符串读取对象时出现 StreamCorruptedException

转载 作者:行者123 更新时间:2023-12-02 05:21:38 25 4
gpt4 key购买 nike

这是我的代码:

 try {

Uri uri = Uri.parse("file:///storage/emulated/0/Download/information.csv");
File file = new File(uri.getPath());
//Read the file
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
boolean firstLine = true;

while ((line = br.readLine()) != null) {
System.out.println("Print the contents from the file :" + line);
if (firstLine) {
firstLine = false;
continue;
} else {

//deserialize the string to Object
td = TransferData.fromString(line); //Where td is an object

//deserialize the string to Object
System.out.println("deserialized: " + td);



}

但是,我在这一行遇到异常:

td = TransferData.fromString(line);

以及来 self 的 fromString 函数:

/** Read the object from Base64 string. */
public static TransferData fromString( String s ) throws IOException, ClassNotFoundException {
byte [] data = Base64.decode(s, Base64.DEFAULT);
ObjectInputStream ois = new ObjectInputStream(
new ByteArrayInputStream( data ) );
Object o = ois.readObject();
ois.close();
return (TransferData)o;
}

异常(exception)是 StreamCorruptedException 但我不确定为什么会这样。我希望能够读取字符串并反序列化该字符串。

编辑:

  /** Write the object to a Base64 string. */
public static String toString(Serializable o) throws IOException {
ByteArrayOutputStream output = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(output);
oos.writeObject(o);
oos.close();
return new String(Base64.encode(output.toByteArray(), Base64.DEFAULT));
}



//SerialVersionID
private static final int serialVersionUID = 10032014;

最佳答案

我可以看到您的代码中的一些缺陷:

  1. 每次读取新行 ( which is probably causing the StreamCorruptedException ) 时,您都会创建一个新的 ObjectInputStream
  2. 如果您通过 BufferedReader 读取文件,则不需要 ObjectInputStream,反之亦然。
  3. 我不明白,为什么要对字符串进行 Base64.decode
  4. 您必须确保serialVersionUID是连贯的。

如果我理解得很好,你想将一个对象序列化到一个文件,然后反序列化它。首先,您应该有一个实现 Serializable 的实体(您的对象)接口(interface):

public class Foo implements Serializable{
static final long serialVersionUID = 42L;
//class code...
}

现在您可以安全地将对象(或其列表)保存到文件中:

FileOutputStream fos = new FileOutputStream("myFoo.tmp");
ObjectOutputStream oos = new ObjectOutputStream(fos);

Foo foo = new Foo();
//Fill the object.

oos.writeObject(foo);

如果您需要读取该文件,您现在可以执行以下操作:

FileInputStream fis = new FileInputStream("myFoo.tmp");
ObjectInputStream ois = new ObjectInputStream(fis);

Foo mySavedFoo = (Foo) ois.readObject();

如果您需要保存对象列表,您可以使用 ArrayList implementation实现 SerializedList :

FileOutputStream fos = new FileOutputStream("myFoos.tmp");
ObjectOutputStream oos = new ObjectOutputStream(fos);

List<Foo> foos = new ArrayList<Foo>();
//Fill the list.

oos.writeObject(foos);

//...

FileInputStream fis = new FileInputStream("myFoos.tmp");
ObjectInputStream ois = new ObjectInputStream(fis);

ArrayList<Foo> mySavedFoo = (ArrayList<Foo>) ois.readObject();

附录

当从对象流读取的控制信息违反内部一致性检查时,会抛出StreamCorruptedException

当使用ObjectOutputStream将对象写入文件时,会添加反序列化对象时使用的格式正确的 header 。如果此类 header 不一致,则违反了内部一致性检查。

当您逐行读取文件时 (line = br.readLine()),您正在删除此类 header ,因此您的 ObjectInputStream 无法正确读取您保存的对象。此外,您正在解码 Base64,从而对读取的数据进行更多加扰。添加为每行创建一个新的 ObjectInputStream 并出现 StreamCorruptedException

<小时/>

编辑

从您的编辑中我可以更好地理解您的问题是什么。

public static String toString(Serializable o) throws IOException {
// what is this? ^
ByteArrayOutputStream output = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(output);
oos.writeObject(o); //Here you are saving a Serializable object to... what? Which file?
oos.close();
return new String(Base64.encode(output.toByteArray(), Base64.DEFAULT));
}

就像我之前的示例一样,您通过 ObjectOutputStream 存储一个 Serializable 对象(BTW 在哪里?),但 Serialized 只是一个接口(interface)。如果你使用实现它的类会更好。按照我的例子,它将变成:

public static String toString(Foo o) throws IOException { //...

Base64.encode 在对象保存到文件之后完成!没有“加密”!此外,编码是对 output 对象(即 ByteArrayOutputStream)完成的,与存储在 o 中的真实数据无关。

关于java - 从字符串读取对象时出现 StreamCorruptedException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26466257/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com