bytearrayinputstream java,java – 使用ObjectInputStream和ByteArrayInputStream的StreamCorruptedException...

本文介绍了在Java中使用ObjectOutputStream和ObjectInputStream进行对象序列化和反序列化时遇到的一个错误:java.io.StreamCorruptedException。问题在于从磁盘读取文件时使用了错误的方法,导致加载的数据不是原始的对象序列化内容。解决方案是使用FileUtils.readFileToByteArray()方法正确读取文件内容,从而成功解码对象。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我使用ObjectOutputStream将许多对象写入磁盘.在阅读期间,出于某些实现原因,我首先将文件检索为ByteArray,我想读取缓冲数组并从中解码数据.这是一段代码片段

byte [] fileArray=org.apache.commons.io.IOUtils.toByteArray(filePath);

ObjectInputStream in=new ObjectInputStream(new ByteArrayInputStream(fileArray));

while(true){

Records pos=(Records)in.readObject();

}

但是,我收到此错误

java.io.StreamCorruptedException: invalid stream header: 2F6C6F63

总之,我想将文件加载到内存中,然后在读取时解码对象而不是磁盘.

该文件写为:

fout=new FileOutputStream(filePath);

bos=new ByteArrayOutputStream();

oos=new ObjectOutputStream(bos);

for(int i=0;i

oos.writeObject(list.get(i));

}

oos.flush();

bos.writeTo(fout);

bos=null;

oos=null;

fout.flush();

fout.close();

oos根本没有关闭!

以下是重现错误的完整示例:

import java.util.*;

import java.io.*;

import org.apache.commons.io.IOUtils.*;

public class Example{

private int[] data;

public Example(){

data=new int[40];

}

public void generate(){

for(int i=0;i

data[i]=i;

}

System.out.println("Data generated!");

}

public void write(){

FileOutputStream fout=null;

ByteArrayOutputStream bos=null;

ObjectOutputStream oos=null;

try{

fout=new FileOutputStream("obj.data");

bos=new ByteArrayOutputStream();

oos=new ObjectOutputStream(bos);

for(int i=0;i

oos.writeObject((Integer)data[i]);

}

oos.flush();

bos.writeTo(fout);

bos=null;

oos=null;

fout.flush();

fout.close();

}catch(IOException ioe){}

System.out.println("Data written!");

}

public void read(){

ObjectInputStream in=null;

try{

byte[] fileArray=org.apache.commons.io.IOUtils.toByteArray("obj.data");

in=new ObjectInputStream(new ByteArrayInputStream(fileArray));

while(true){

Integer data=(Integer)in.readObject();

}

}catch (EOFException eofe){

try{

in.close();

}catch (IOException ioe){

ioe.printStackTrace();

}

}catch(ClassNotFoundException e){

e.printStackTrace();

}catch (Exception e){

e.printStackTrace();

}

System.out.println("Data read!");

}

public static void main(String[] args){

Example example=new Example();

example.generate();

example.write();

example.read();

}

}

解决方法:

好的,现在就找到了.这就是问题:

byte[] fileArray=org.apache.commons.io.IOUtils.toByteArray("obj.data");

That method没有按照你的想法做到:

Get the contents of a String as a byte[] using the default character encoding of the platform.

它根本没有加载文件.

如果你改用它:

byte[] fileArray =

org.apache.commons.io.FileUtils.readFileToByteArray(new File("obj.data"));

…然后正确恢复数据.

(就我个人而言,我更喜欢Guava这种东西……)

标签:java,serialization,deserialization,java-io

来源: https://codeday.me/bug/20190629/1329240.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值