/*
One of the most useful aspects of properties is that the store () method and the load () method can be used to conveniently store information contained in the properties object or load information from the disk. A property object can be written to or read from the stream at any time, which makes the attribute list easy to implement a simple database.
The following program uses the property list to create a simple phone book that stores the name and number. You can enter a name to search for a person's phone number. The program uses the store () and load () Methods to store and retrieve lists. When the program is executed, it starts from a phonebook. the DAT file is included in the list. If this file exists, the list will be loaded and the list will be added. If this file does not exist, it will be created.
*/
/*
A simple telephone number database that uses a property list.
*/
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 strname, strnumber;
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 exsiting
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 ):");
Strname = Br. Readline ();
If (strname. Equals ("quit") continue;
System. Out. println ("Enter number :");
Strnumber = Br. Readline ();
Ht. Put (strname, strnumber );
Changed = true;
} While (! Strname. Equals ("quit "));
// If phonebook 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 stop ):");
Strname = Br. Readline ();
If (strname. Equals ("quit") continue;
Strnumber = (string) Ht. Get (strname );
System. Out. println (strnumber );
} While (! Strname. Equals ("quit "));
}
}