我正在尝试写入一个文件,然后再次读取它。
我写的代码:
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)
有人可以帮忙吗?
最佳答案
您使用java的ObjectOutputStream
来序列化您的对象,但使用一些涉及 Guava 的自定义反序列化?
如果您使用 ObjectOutputStream
来编写内容,则应该使用 ObjectInputStream阅读它。另外,你的例子没有任何意义。您永远不会在序列化片段中初始化 myObject
变量,此代码将根本无法编译。
关于java - 无效 Protocol Buffer 异常 : Protocol message end-group tag did not match expected tag,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25671458/