Java Properties class usage basics, javaproperties
The Properties class inherits from HashTable and is usually used in combination with io streams. The most prominent feature is that the key/value is written to the configuration file as the configuration property for configuration persistence or read from the configuration file. The standard Suffix of these configuration files is ". properties ". Represents a persistent property set.
Notes:
If you want to write the property set in the Properties set to the configuration file, use the store () method. If you want to read the property from the ". properties" configuration file, you can use the load () method.
The following are common methods of the Properties class:
setProperty(String k,String v)
: Call the put Method of hashtable to add key/value to the properties set. The returned value is the old value corresponding to the key. If no old value exists, Null is returned. Note that both k and v are of the String type.
getProperty(String k)
: Get the value corresponding to the key in the properties set.
store(OutputStream o,String comment)
: Write the set of properties to the output stream o. Note that comment comments are required. -load(InputStream i)
: Read the properties in byte from the. properties configuration file.
load(Reader r)
: Read the properties from the. properties configuration file according to characters.
stringPropertyNames()
: Returns the Set composed of keys in the properties Set.
The following is a simple example of adding, retrieving, and traversing key/value to the properties set, and combining with the IO stream.
Import java. util. *; import java. io. *; public class Prop {public static void main (String [] args) throws IOException {Properties prop = new Properties (); // store key/value prop to the properties set prop. setProperty ("filename", ". avi "); prop. setProperty ("size", "5 M"); // The format of the key/value stored in the prop set is System. out. println (prop); // extracts a single key/value prop from prop. getProperty ("filename"); // traverses the prop Set <String> keys = prop. stringPropertyNames (); for (String key: keys) {String value = prop. getProperty (key); System. out. println (key + "=" + value);} // properties set and IO output stream set: write the property set in the prop set to the file for persistent FileOutputStream fos = new FileOutputStream ("d:/temp/my. properties "); prop. store (fos, "store test"); // properties set and IO output stream set: read the property set from the properties file to the prop1 collection // FileInputStream FD = new FileInputStream ("d:/temp/my. properties "); FileReader fr = new FileReader (" d:/temp/my. properties "); Properties prop1 = new Properties (); // Now it's a Null properties prop1.load (fr); System. out. println ("new prop:" + prop1); fos. close (); fr. close ();}}
Note: If you think this article is not bad, please click the recommendation in the lower right corner. Your support can stimulate the author's enthusiasm for writing. Thank you very much!