0

我有一个 java 可序列化对象,我想将该对象写入/读取文件。然而 Google 应用引擎不支持 fileoutputstream 和 objectoutputstream 所以我将可序列化对象转换为字节数组并将其写入文件

$FileWriteChannel writeChannel = fileService.openWriteChannel(
writableFile, lockForWrite);
$writeChannel.write(ByteBuffer.wrap(bytearray));
$writeChannel.closeFinally();

然后我读了文件:

AppEngineFile readableFile = new AppEngineFile(filename);
FileReadChannel readChannel = fileService.openReadChannel(readableFile, lockForRead);
BufferedReader reader = new BufferedReader(Channels.newReader(readChannel, "UTF-8"));
String line = reader.toString();

我看到了日志,但出现了 UTF-8 错误 谁能告诉我我做错了什么?
错误日志:

cloud.spam.filter.server.SerializationObject file_io: An error message: invalid stream header: 5B4240312012/07/06 08:00:10

谢谢

4

1 回答 1

0

考虑为此目的使用 protobuf。除了它比标准 java 序列化格式更紧凑的事实之外,它还使您能够更改(通过添加新字段)要写入文件的类。

于 2012-07-07T16:09:58.050 回答