1 // See README.txt for information and build instructions.
3 import com.example.tutorial.AddressBookProtos.AddressBook;
4 import com.example.tutorial.AddressBookProtos.Person;
5 import java.io.FileInputStream;
6 import java.io.IOException;
7 import java.io.PrintStream;
10 // Iterates though all people in the AddressBook and prints info about them.
11 static void Print(AddressBook addressBook) {
12 for (Person person: addressBook.getPersonList()) {
13 System.out.println("Person ID: " + person.getId());
14 System.out.println(" Name: " + person.getName());
15 if (person.hasEmail()) {
16 System.out.println(" E-mail address: " + person.getEmail());
19 for (Person.PhoneNumber phoneNumber : person.getPhoneList()) {
20 switch (phoneNumber.getType()) {
22 System.out.print(" Mobile phone #: ");
25 System.out.print(" Home phone #: ");
28 System.out.print(" Work phone #: ");
31 System.out.println(phoneNumber.getNumber());
36 // Main function: Reads the entire address book from a file and prints all
37 // the information inside.
38 public static void main(String[] args) throws Exception {
39 if (args.length != 1) {
40 System.err.println("Usage: ListPeople ADDRESS_BOOK_FILE");
44 // Read the existing address book.
45 AddressBook addressBook =
46 AddressBook.parseFrom(new FileInputStream(args[0]));