gpt4 book ai didi

java - 替换 Google 协议(protocol)数据缓冲区中的字符串

转载 作者:行者123 更新时间:2023-12-01 10:22:03 25 4
gpt4 key购买 nike

我已成功构建以下 .proto 文件:

package JervisStorage;

option java_package = "TextBase";
option java_outer_classname = "JervisStorage";

message Owner {
optional string name = 1;
optional string sex = 2;
optional string profession = 3;
optional string email = 4;
}

现在,我成功构建了所有者:

private static Owner owner;
private static FileOutputStream serialOutput;

Owner pusheen= Owner.newBuilder()
.setName("Siema")
.setSex(" ")
.setProfession(" ")
.setEmail(" ")
.build();

我将对象写入文件并成功从文件中读取对象:

serialOutput = new FileOutputStream("JervisStorage.ser");
pusheen.writeTo(serialOutput);
serialOutput.close();

owner = Owner.parseFrom(new FileInputStream("JervisStorage.ser"));
System.out.println(owner.getName());

问题是我无法替换单条记录,将其写回文件并读取整个更新的对象。我一直在尝试这样做:

owner.toBuilder().setName("newName").build();
System.out.println(owner.getName());
serialOutput = new FileOutputStream("JervisStorage.ser");
owner.writeTo(serialOutput);
serialOutput.close();

owner = Owner.parseFrom(new FileInputStream("JervisStorage.ser"));
System.out.println(owner.getName());

不幸的是,这种方法似乎不起作用......有人可以帮忙吗?

最佳答案

为什么不这样做

  editedOwner = Owner.newBuilder()
.mergeFrom(new FileInputStream("JervisStorage.ser"))
.setName("new name")
.build();

或者你也可以这样做

  editedOwner = owner.toBuilder()
.setName("new name")
.build();

关于java - 替换 Google 协议(protocol)数据缓冲区中的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35530853/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com