资源简介
这是我从官网下载的最新的源码包(https://developers.google.com/protocol-buffers/docs/downloads)那些上不了google的同学,那这个吧
代码片段和文件信息
// 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();
}
}
}
相关资源
- hostapd-2.0.tar.gz
- pkg-config-0.26.tar.gz
- AnkhSvn-2.6.12735 支持VS2010-VS2015 官方原版
- AnkhSvn-2.6.12707
- rubygems-2.6.11.tgz
- sphinx中文分词 xdict_1.1.tar.gz
- libconfig-1.4.9.tar.gz
- pip-10.0.1.tar.gz
- rtl8188FU_linux_v5.2.11.1_22924.20170703.tar.g
- ffmpeg-2.8.14.tar.gz
- tfn2k.tar.gz
- kinetics600.tar.gz
- perl-5.24.0.tar.gz
- openssh-7.7p1.tar.gz
- nexus-2.14.3-02-bundle.tar.gz
- git-2.0.0.tar.gz
-
libxm
l2-2.9.4-rc2.tar.gz - lua-sha2-0.2.0.tar.gz
- linux2.6.11.12内核源码注释
- awl-0.2.tar.gz TCP SYN洪水攻击 linux工具
- 鑫河软件IP地址自动更换器 v2.6.18
- apr-1.5.2.tar.gz+apr-util-1.5.2.tar.gz
- 用RNN与LSTM网络原理进行唐诗生成.ta
- code.tar.gz
- bochs-2.4.5.tar.gz
- libpng-1.6.29.tar.gz
- redis-5.0.2.tar.gz
- apr-iconv-1.2.1.tar.gz 、apr-util-1.5.4.tar.g
- protobuf-2.5.0269634
- cmake-2.8.10.2.tar.gz
评论
共有 条评论