我正在尝试写入文件,然后再次读取它。InvalidProtocolBufferException:协议消息结束标记标记与预期标记不匹配
我写代码:
ObjectOutputStream oos = null;
FileOutputStream fout = null;
try
{
Object myObject;
fout = new FileOutputStream(new File("C:\\Foo","Bar.log"));
oos = new ObjectOutputStream(fout);
oos.writeObject(myObject);
} catch (Exception ex) {
ex.printStackTrace();
} finally {
try {
oos.close();
fout.close();
} catch (IOException e) {
e.printStackTrace();
}
}
我的读者:
FileInputStream input;
try {
input = new FileInputStream(new File("C:\\Foo\\Bar.log"));
MyFile parsedObject = MyFileFormat.MyFile.parseFrom(input);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
但我得到的解析器此异常:
com.google.protobuf.InvalidProtocolBufferException: Protocol message end-group tag did not match expected tag.
at com.google.protobuf.InvalidProtocolBufferException.invalidEndTag(InvalidProtocolBufferException.java:94)
谁能帮助?
您已经标记了这个“protobuf-net”,但这不可能是正确的,它只能在.Net上运行。 – RenniePet 2014-09-04 18:35:38
我在这里可以想到的最可能的事情是,你用一个*更小的*文件覆盖了一个文件,而不会截断它 - 因此在最后留下垃圾。那可能吗? – 2014-09-04 19:53:20