protobuf/examples/ListPeople.java
Go to the documentation of this file.
1 // See README.txt for information and build instructions.
2 
3 import com.example.tutorial.protos.AddressBook;
4 import com.example.tutorial.protos.Person;
5 import java.io.FileInputStream;
6 import java.io.IOException;
7 import java.io.PrintStream;
8 
9 class ListPeople {
10  // Iterates though all people in the AddressBook and prints info about them.
11  static void Print(AddressBook addressBook) {
12  for (Person person: addressBook.getPeopleList()) {
13  System.out.println("Person ID: " + person.getId());
14  System.out.println(" Name: " + person.getName());
15  if (!person.getEmail().isEmpty()) {
16  System.out.println(" E-mail address: " + person.getEmail());
17  }
18 
19  for (Person.PhoneNumber phoneNumber : person.getPhonesList()) {
20  switch (phoneNumber.getType()) {
21  case MOBILE:
22  System.out.print(" Mobile phone #: ");
23  break;
24  case HOME:
25  System.out.print(" Home phone #: ");
26  break;
27  case WORK:
28  System.out.print(" Work phone #: ");
29  break;
30  default:
31  System.out.println(" Unknown phone #: ");
32  break;
33  }
34  System.out.println(phoneNumber.getNumber());
35  }
36  }
37  }
38 
39  // Main function: Reads the entire address book from a file and prints all
40  // the information inside.
41  public static void main(String[] args) throws Exception {
42  if (args.length != 1) {
43  System.err.println("Usage: ListPeople ADDRESS_BOOK_FILE");
44  System.exit(-1);
45  }
46 
47  // Read the existing address book.
48  AddressBook addressBook =
49  AddressBook.parseFrom(new FileInputStream(args[0]));
50 
51  Print(addressBook);
52  }
53 }
testing::gtest_printers_test::Print
std::string Print(const T &value)
Definition: bloaty/third_party/googletest/googletest/test/googletest-printers-test.cc:233
asyncio_get_stats.args
args
Definition: asyncio_get_stats.py:40
main
int main(int argc, char **argv)
Definition: examples/cpp/compression/greeter_client.cc:78
ListPeople
void ListPeople(const tutorial::AddressBook &address_book)
Definition: bloaty/third_party/protobuf/examples/list_people.cc:15
java
com


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:00:28