PropertiesIntroduction to Classes
the properties class represents a persistent set of attributes. Properties can be saved in a stream or loaded from a stream . each key and its corresponding value in the property list is a string . A property list can contain another property list as its default value, or a second property list if it fails to search for the property key in the original property list. becausePropertiesinherit fromHashtable, so you canPropertiesObject Applicationputand theputall method. However, these two methods are not recommended because they allow the caller to insert their key or the value is not string . Instead, you should use the setproperty method . If you invoke the "unsafe" propertiesstringstoresave method, the call will fail. Similarly, if the "unsafe" propertiesstringpropertynameslist method, the call will fail.
How to construct the properties class
Properties () creates a list of empty properties with no default values. Properties (properties defaults) creates a list of empty properties with the specified default value.
Use of properties
Because properties is a subclass of Hashtable<object,object> , we can view it as a collection of maps, You can store Key-value key-value pairs, except that the properties want you to store all the key values of the string, and we cannot define generics when we define them, because the default is to expect you to store <String,String>. However, since it inherits the Hashtable<Object,object> method, we can put the non-string key value with the Put method. For example, the following lines of code will not be an error:
Public Static void throws IOException { new Properties (); P.put (1111, 2222); P.setproperty ("AAA", "1"); P.setproperty ("BBB", "2"); P.setproperty ("CCC", "3"); System.out.println (P.getproperty ("AAA")); System.out.println (P.get (1111)); }
But the most wonderful feature that distinguishes the properties from Hashtable is the ability to dock streams. Here are a few ways to see:
voidList (PrintStream out) outputs the list of attributes to the specified output stream. voidList (printwriter out) outputs the list of attributes to the specified output stream. void load(inputstream instream) reads the list of attributes (key and element pairs) from the input stream. void load(Reader Reader) reads the list of attributes (key and element pairs) from the input character stream in a simple line-oriented format. voidLoadFromXML (InputStream in) loads all the properties represented by the XML document in the specified input stream into this property sheet. void Store(OutputStream out, String comments) is used in a format that is suitable for loading into the Properties table using the Load (InputStream) method, which Prop The list of attributes (key and element pairs) in the Erties table is written to the output stream. void Store(writer writer, String comments) writes the list of attributes (key and element pairs) in this properties table to the output in a format that is appropriate for using the load (Reader) method Character. void storetoxml(outputstream os, String comment) emits an XML document that represents all of the properties contained in this table. voidstoretoxml (outputstream os, string comment, string encoding) emits an XML document that represents all the attributes contained in this table using the specified encoding.
This means that the properties can directly write the properties to a file or load it into a program by docking the stream. For example, the following write code:
Public Static void throws IOException { new Properties (); New FileWriter ("T.txt"); P.setproperty ("AAA", "1"); P.setproperty ("BBB", "2"); P.setproperty ("CCC", "3"); P.store (writer,"set properties"); W.close (); SYSTEM.OUT.PRINTLN (P); }
The last file in the T.txt is shown in this way:
16:20:01 HKT,BBB=2AAA=1CCC=3
Of course, you can also load a property by using the Load method:
Public Static void throws IOException { new Properties (); New FileReader ("T.txt"); P.load (reader);
R.close (); System.out.println (P.getproperty ("AAA")); SYSTEM.OUT.PRINTLN (P); }
Persistent attribute set properties in Java Basics Java