java. properties file use case source

Source: Internet
Author: User

First, describe

The correct use of the. Properties property file in Java can solve many problems, such as a login interface to remember the user's logged in user name and password and put on the local user-friendly login.

Second, the Operation procedure

1. Open the Xx.properties file in the Eclipse Engineering files directory, and if not, create a

2. record the user's most recently logged on user name in key-value pairs--password, add a key value pair

3. Remove a key-value pair

4. Save this property file

5. Get all the keys for a property file

6. Get The property value of the specified key

Second, the source code

Create a property file under the Working home directory (that is, the Eclipse project directory, for example, to record the user name and password that the user recently logged in) file File = new file (System.getproperty ("User.dir") + "\ \ Login.properties "), if (!file.exists ()) {//If the file does not exist, first create file.createnewfile ();} Properties.load (new FileInputStream (file)); Load properties File//Get a property in the properties file, <span style= "font-family:arial; line-height:20px; Background-color:rgb (255, 255, 255); " > Search property value for the specified key in this attribute </span>if (Properties.getproperty ("firstname") = = null) {// Set a property in the properties file Properties.setproperty ("FirstName", "");} arraylist<string> accountlist = new arraylist<string> (), @SuppressWarnings ("unchecked")//Get all keys of the properties file, Put in an enumeration type of String type enumeration<string> en = (enumeration<string>) properties.propertynames (); while ( En.hasmoreelements ()) {string key = (string) en.nextelement (); Accountlist.add (key);} Convert arraylist<string> to a string array, place it in a drop-down list for user selection string[] namehistory = (string[]) Accountlist.toarray (new string [1]); JComboBox Accountbox = new JComboBox (namehistory);//Create a new key-value pair, record the last logon name as the preferred login, and the default selection for each login properTies.put ("FirstName", sname);p roperties.remove (keyName); Delete a record of the key KeyName//Save the properties file and catch the appropriate exception try {Properties.store (new FileOutputStream ("login.properties"), null);} catch ( FileNotFoundException ex) {Logger.getlogger (LoginFrame.class.getName ()). log (Level.severe, NULL, ex);} catch ( IOException ex) {Logger.getlogger (LoginFrame.class.getName ()). log (Level.severe, NULL, ex);}


java. properties file use case source

Related Article

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.