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