Example of the store () method and load () method of Properties

Source: Internet
Author: User

/*

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 "));
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.