资源简介
protocolbuffer 以下简称PB 是google 的一种数据交换的格式 它独立于语言 独立于平台 google 提供了三种语言的实现:java c++ 和 python
代码片段和文件信息
// See README.txt for information and build instructions.
import com.example.tutorial.AddressBookProtos.AddressBook;
import com.example.tutorial.AddressBookProtos.Person;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.IOException;
import java.io.PrintStream;
class AddPerson {
// This function fills in a Person message based on user input.
static Person PromptForAddress(BufferedReader stdin
PrintStream stdout) throws IOException {
Person.Builder person = Person.newBuilder();
stdout.print(“Enter person ID: “);
person.setId(Integer.valueOf(stdin.readLine()));
stdout.print(“Enter name: “);
person.setName(stdin.readLine());
stdout.print(“Enter email address (blank for none): “);
String email = stdin.readLine();
if (email.length() > 0) {
person.setEmail(email);
}
while (true) {
stdout.print(“Enter a phone number (or leave blank to finish): “);
String number = stdin.readLine();
if (number.length() == 0) {
break;
}
Person.PhoneNumber.Builder phoneNumber =
Person.PhoneNumber.newBuilder().setNumber(number);
stdout.print(“Is this a mobile home or work phone? “);
String type = stdin.readLine();
if (type.equals(“mobile“)) {
phoneNumber.setType(Person.PhoneType.MOBILE);
} else if (type.equals(“home“)) {
phoneNumber.setType(Person.PhoneType.HOME);
} else if (type.equals(“work“)) {
phoneNumber.setType(Person.PhoneType.WORK);
} else {
stdout.println(“Unknown phone type. Using default.“);
}
person.addPhone(phoneNumber);
}
return person.build();
}
// Main function: Reads the entire address book from a file
// adds one person based on user input then writes it back out to the same
// file.
public static void main(String[] args) throws Exception {
if (args.length != 1) {
System.err.println(“Usage: AddPerson ADDRESS_BOOK_FILE“);
System.exit(-1);
}
AddressBook.Builder addressBook = AddressBook.newBuilder();
// Read the existing address book.
try {
FileInputStream input = new FileInputStream(args[0]);
try {
addressBook.mergeFrom(input);
} finally {
try { input.close(); } catch (Throwable ignore) {}
}
} catch (FileNotFoundException e) {
System.out.println(args[0] + “: File not found. Creating a new file.“);
}
// Add an address.
addressBook.addPerson(
PromptForAddress(new BufferedReader(new InputStreamReader(System.in))
System.out));
// Write the new address book back to disk.
FileOutputStream output = new FileOutputStream(args[0]);
try {
addressBook.build().writeTo(output);
} finally {
output.close();
}
}
}
相关资源
- .proto 各种语言转换
- Protobuf-3.3生成器PB和java资源.zip
- google protobuf 开发指南中文版
- 基于netty与protobuf的Android手机视频实时
- protoc-3.9.1-linux-x86_64.zip
- Unity5Google Protobuf解析工具,支持andr
- Protobuf测试工程(包括Unity和Java服务端
- protobuf-java-3.4.1.jar
- protobuf-java-2.6.0.jar
- protobuf-2.5.0 exe文件
- protobuf的protoc.exe和jar
- protobuf-2.5.0-windows-环境包
- protobuf-java-3.2.0.jar
- protoc-3.0.2-win32.zip
- protobuf-master(20191007).zip
- protobuf-2.5.0 jar包及代码生成工具
- google.protobuf.dll
- protobuf-java-3.1.0.jar
- protobuf-java-3.6.1.jar
- Java 和 protobuf 3.5开发一
- java服务端,C++客户端,基于protobuf的
评论
共有 条评论