Here is a Java Program to A simple telephone number database that uses a property list.
Output of Above Java Program
sanjay
Enter number:
9977777516
Enter new name ('quit' to stop):
Hell
Enter number:
442312345348
Enter new name ('quit' to stop):
ASDF
Enter number:
4647511366489
Enter new name ('quit' to stop):
quit
Enter name to find ('quit' to quit):
sanjay
9977777516
Enter name to find ('quit' to quit):
asdf
null
Enter name to find ('quit' to quit):
ASDF
4647511366489
Enter name to find ('quit' to quit):
quit
import java.io.*; import java.util.*; class Phonebook { public static void main(String args[]) throws IOException { Properties ht = new Properties(); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String name, number; FileInputStream fin = null; boolean changed = false; // Try to open phonebook.dat file. try { fin = new FileInputStream("phonebook.dat"); } catch(FileNotFoundException e) { // ignore missing file } /* If phonebook file already exists, load existing telephone numbers. */ try { if(fin != null) { ht.load(fin); fin.close(); } } catch(IOException e) { System.out.println("Error reading file."); } // Let user enter new names and numbers. do { System.out.println("Enter new name" + " ('quit' to stop): "); name = br.readLine(); if(name.equals("quit")) continue; System.out.println("Enter number: "); number = br.readLine(); ht.put(name, number); changed = true; } while(!name.equals("quit")); // If phone book data has changed, save it. if(changed) { FileOutputStream fout = new FileOutputStream("phonebook.dat"); ht.store(fout, "Telephone Book"); fout.close(); } // Look up numbers given a name. do { System.out.println("Enter name to find" + " ('quit' to quit): "); name = br.readLine(); if(name.equals("quit")) continue; number = (String) ht.get(name); System.out.println(number); } while(!name.equals("quit")); } }
Output of Above Java Program
sanjay
Enter number:
9977777516
Enter new name ('quit' to stop):
Hell
Enter number:
442312345348
Enter new name ('quit' to stop):
ASDF
Enter number:
4647511366489
Enter new name ('quit' to stop):
quit
Enter name to find ('quit' to quit):
sanjay
9977777516
Enter name to find ('quit' to quit):
asdf
null
Enter name to find ('quit' to quit):
ASDF
4647511366489
Enter name to find ('quit' to quit):
quit