(String[] args)
| 60 | // adds one person based on user input, then writes it back out to the same |
| 61 | // file. |
| 62 | public static void main(String[] args) throws Exception { |
| 63 | if (args.length != 1) { |
| 64 | System.err.println("Usage: AddPerson ADDRESS_BOOK_FILE"); |
| 65 | System.exit(-1); |
| 66 | } |
| 67 | |
| 68 | AddressBook.Builder addressBook = AddressBook.newBuilder(); |
| 69 | |
| 70 | // Read the existing address book. |
| 71 | try { |
| 72 | FileInputStream input = new FileInputStream(args[0]); |
| 73 | try { |
| 74 | addressBook.mergeFrom(input); |
| 75 | } finally { |
| 76 | try { input.close(); } catch (Throwable ignore) {} |
| 77 | } |
| 78 | } catch (FileNotFoundException e) { |
| 79 | System.out.println(args[0] + ": File not found. Creating a new file."); |
| 80 | } |
| 81 | |
| 82 | // Add an address. |
| 83 | addressBook.addPeople( |
| 84 | PromptForAddress(new BufferedReader(new InputStreamReader(System.in)), |
| 85 | System.out)); |
| 86 | |
| 87 | // Write the new address book back to disk. |
| 88 | FileOutputStream output = new FileOutputStream(args[0]); |
| 89 | try { |
| 90 | addressBook.build().writeTo(output); |
| 91 | } finally { |
| 92 | output.close(); |
| 93 | } |
| 94 | } |
| 95 | } |
no test coverage detected