Properties class inherits from Hashtable
It provides several main methods:
1. GetProperty (String key) that searches for properties in this property list with the specified key. That is, by the parameter key, the value corresponding to the key is obtained.
2. Load (InputStream instream) to read the list of attributes (key and element pairs) from the input stream. Gets all the key-value pairs in the file by loading the specified file. To search for GetProperty (String key).
3. SetProperty (string key, String value), calls the Hashtable method put. He sets a key-value pair by calling the put method of the base class.
4. Clear () Clears all loaded key-value pairs. This method is provided in the base class.
Example:
1 Public Static voidMain (string[] args)2 {3 Try4 {5 //get the current project path6Properties props =system.getproperties ();7String ProjectPath = Props.getproperty ("User.dir");8 9 //read the properties fileTenProperties pps =NewProperties (); OneFileInputStream InputStream =NewFileInputStream (ProjectPath + "\\src\\test.properties"); A pps.load (inputstream); - - //read value by key theSystem.out.println (Pps.getproperty ("name")); -System.out.println (Pps.getproperty ("Score")); - } - Catch(IOException e) + { - //TODO auto-generated Catch block + e.printstacktrace (); A } at}
Java Learning (21): Reading of the properties configuration file