从序列化文件读取数据会引发java.io.StreamCorruptedException:
我正在将一个对象的arraylist转储到一个文件中。我正在逐个对象序列化对象并登录到一个文件中,而不是整个arraylist序列化,因为arraylist的数量可以超过一个。该过程工作正常,数据正在追加到文件中,但在反序列化过程中,我只能检索第一个arraylist对象,一旦第二个arraylist对象出现,它就会抛出java.io.StreamCorruptedException:无效类型代码:AC。我的每个arraylist都有相同类型的对象。我已经尝试了很多搜索,但是像重置、AppendableObjectoutputstream这样的技巧都不起作用 我的序列化代码是:从序列化文件读取数据会引发java.io.StreamCorruptedException:,java,file,serialization,Java,File,Serialization,我正在将一个对象的arraylist转储到一个文件中。我正在逐个对象序列化对象并登录到一个文件中,而不是整个arraylist序列化,因为arraylist的数量可以超过一个。该过程工作正常,数据正在追加到文件中,但在反序列化过程中,我只能检索第一个arraylist对象,一旦第二个arraylist对象出现,它就会抛出java.io.StreamCorruptedException:无效类型代码:AC。我的每个arraylist都有相同类型的对象。我已经尝试了很多搜索,但是像重置、Append
publicstaticvoiddumploginfile(arraylistsanitizelog)
{
尝试
{
String fileName=“c:\\temp\\auditinfo.ser”;
FileOutputStream文件输出=
新FileOutputStream(文件名,true);
ObjectOutputStream out=新的ObjectOutputStream(fileOut);
对于(HTMLElementoObject eachobj:sanitizelog)
{
out.writeObject(eachobj);
out.reset();
}
out.close();
fileOut.close();
System.out.printf(“序列化数据保存在/tmp/auditinfo.ser中”);
//logElementData.clear();
}捕获(IOI异常)
{
i、 printStackTrace();
}
}
在这里,arraylist的数量将更多,而不是一个,因为它的工作方式是这样的:如果webservice失败,它将转储文件中的数据
从文件读取数据的反序列化代码
publicstaticarraylistextractlogfromfile()
{
HtmlElementObject v;
ArrayList extractLogList=新建ArrayList();
尝试
{
FileInputStream fileIn=newfileinputstream(“c:\\temp\\auditinfo.ser”);
ObjectInputStream in=新的ObjectInputStream(fileIn);
//ObjectInputStream in=新的ObjectInputStream(fileIn);
/*.readObject()中的ArrayList arr=(ArrayList);
//对于(HTMLElementoObject a:arr)
// {
//.readObject()中的a=(HtmlElementObject);
//删除日志列表。添加(a);
//}*/
整数计数=0;
而((v=(HtmlElementObject)in.readObject())!=null)
{
System.out.println(count++);
HTMLElementoObject a;
a=v;/(HtmlElementObject)在.readObject()中;
删除日志列表。添加(a);
}
in.close();
fileIn.close();
}捕获(IOI异常)
{
i、 printStackTrace();
返回日志列表;
}捕获(ClassNotFoundException c)
{
System.out.println(“未找到类”);
c、 printStackTrace();
返回null;
}
返回日志列表;
}
}
现在,此函数将轻松读取第一个arraylist的所有对象,但随着新arraylist对象的出现,它会抛出java.io.StreamCorruptedException。如果没有特殊技巧来抑制ObjectOutputStream构造函数编写的第二个流头,则无法附加到对象流文件。您的ObjectInputStream在需要对象的地方遇到了这个问题,并且出现了问题
public static void dumplogInFile(ArrayList<HtmlElementObject> sanitizelog)
{
try
{
String fileName = "c:\\temp\\ auditinfo.ser";
FileOutputStream fileOut =
new FileOutputStream(fileName , true);
ObjectOutputStream out = new ObjectOutputStream(fileOut);
for(HtmlElementObject eachobj : sanitizelog)
{
out.writeObject(eachobj);
out.reset() ;
}
out.close();
fileOut.close();
System.out.printf("Serialized data is saved in /tmp/auditinfo.ser");
//logElementData.clear();
}catch(IOException i)
{
i.printStackTrace();
}
}
public static ArrayList<HtmlElementObject> extractLogFromFile()
{
HtmlElementObject v ;
ArrayList<HtmlElementObject> extractLogList = new ArrayList<HtmlElementObject>();
try
{
FileInputStream fileIn = new FileInputStream("c:\\temp\\ auditinfo.ser");
ObjectInputStream in = new ObjectInputStream(fileIn);
// ObjectInputStream in = new ObjectInputStream(fileIn);
/* ArrayList<HtmlElementObject> arr = (ArrayList<HtmlElementObject>) in.readObject();
// for (HtmlElementObject a : arr)
// {
//a = (HtmlElementObject) in.readObject();
//extractLogList.add(a);
//}*/
int count =0;
while( (v = (HtmlElementObject)in.readObject()) != null)
{
System.out.println(count++);
HtmlElementObject a;
a = v;//(HtmlElementObject) in.readObject();
extractLogList.add(a);
}
in.close();
fileIn.close();
}catch(IOException i)
{
i.printStackTrace();
return extractLogList ;
}catch(ClassNotFoundException c)
{
System.out.println(" class not found");
c.printStackTrace();
return null ;
}
return extractLogList;
}
}