Java 从字符串读取对象时发生StreamCorruptedException
这是我的密码:Java 从字符串读取对象时发生StreamCorruptedException,java,android,serialization,Java,Android,Serialization,这是我的密码: 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(ne
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);
从我的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;
}
例外是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;
我可以看到您的代码中存在一些缺陷:
BufferedReader
读取文件,则不需要ObjectInputStream
和反之亦然Base64.decode
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();
如果需要保存对象列表,可以使用实现可序列化的列表:
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();
就像我前面的示例一样,您通过ObjectOutputStream
(顺便说一下?)存储Serializable
对象,但是Serializable
只是一个接口。如果使用实现它的类,效果会更好。按照我的例子,它将变成:
public static String toString(Foo o) throws IOException { //...
Base64.encode
是在对象保存到文件后完成的!!没有“加密”!此外,对输出
对象进行编码,该对象是由tearrayoutputstream生成的,与o
中存储的真实数据无关。我可以看到您的代码中存在一些缺陷:
BufferedReader
读取文件,则不需要ObjectInputStream
和反之亦然Base64.decode
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();
如果需要保存对象列表,可以使用实现可序列化的列表:
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();
就像我前面的示例一样,您通过ObjectOutputStream
(顺便说一下?)存储Serializable
对象,但是Serializable
只是一个接口。如果使用实现它的类,效果会更好。按照我的例子,它将变成:
public static String toString(Foo o) throws IOException { //...
Base64.encode
是在对象保存到文件后完成的!!没有“加密”!此外,对输出
对象进行编码,该对象是由tearrayoutputstream生成的,与o
中存储的真实数据无关。我可以看到您的代码中存在一些缺陷:
BufferedReader
读取文件,则不需要ObjectInputStream
和反之亦然Base64.decode
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();
如果需要保存对象列表,可以使用实现可序列化的列表:
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();
就像我前面的示例一样,您通过ObjectOutputStream
(顺便说一下?)存储Serializable
对象,但是Serializable
只是一个接口。如果使用实现它的类,效果会更好。按照我的例子,它将变成:
public static String toString(Foo o) throws IOException { //...
Base64.encode
是在对象保存到文件后完成的!!没有“加密”!此外,对输出
对象进行编码,该对象是由tearrayoutputstream生成的,与o
中存储的真实数据无关。我可以看到您的代码中存在一些缺陷:
BufferedReader
读取文件,则不需要ObjectInputStream
和反之亦然Base64.decode
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();
如果需要保存对象列表,可以使用实现可序列化的列表:
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();
就像我前面的示例一样,您通过ObjectOutputStream
(顺便说一下?)存储Serializable
对象,但是Serializable
只是一个接口。如果使用实现它的类,效果会更好。按照我的例子,它将变成:
public static String toString(Foo o) throws IOException { //...
Base64.encode
是在对象保存到文件后完成的!!没有“加密”!此外,编码是对输出
对象进行的,该对象是由tearrayoutputstream生成的,无需任何操作