Properties: (Configuration information Class) is a collection that represents persistence, inherits Hashtable, and the stored value is a key-worthwhile way
Mainly used for production configuration files and read profile information.
A simple example:
1 Importjava.io.FileNotFoundException;2 ImportJava.io.FileReader;3 ImportJava.io.FileWriter;4 Importjava.io.IOException;5 ImportJava.util.Map.Entry;6 Importjava.util.Properties;7 ImportJava.util.Set;8 9 Public classProperties {Ten One Public Static voidMain (string[] args)throwsIOException { A Createpropert (); - Readpropert (); - } the - //(i) Create the configuration information class - Public Static voidCreatepropert ()throwsIOException { - //1. Create an Object +Properties pt =NewProperties (); - + //2. Configure object information (key values are string types) APt.setproperty ("The Junior", "1101"); atPt.setproperty ("Small Two", "1102"); -Pt.setproperty ("Zhang 3", "1103"); -Pt.setproperty ("Lisi", "1104"); - - //3. Save the configured object files to disk (two methods are OK) - //(1) Store (new FileWriter ("c:\\.")) A character stream is recommended if you need to write to Chinese in //(2) Store (new FileOutputStream (c:\\.)) BYTE Stream -Pt.store (NewFileWriter ("c:\\users\\bigerf\\desktop\\ Configuration Flow. Properties"), "This is a descriptive message for the file:"); to } + - the //(ii) Read the configuration object information * Public Static voidReadpropert ()throwsFileNotFoundException, IOException { $ //1. Create an ObjectPanax NotoginsengProperties pt =NewProperties (); - the //2. Read the Configuration object data according to the path load (new FileReader ("c:\\.")) +Pt.load (NewFileReader ("c:\\users\\bigerf\\desktop\\ Configuration flow. Properties")); A the //3. Traversing the collection (Configuration object data) + //A collection is a loop that can be traversed through a foreach -Set<entry<object, object>> Entrys =Pt.entryset (); $ for(Entry<object, object>Entry:entrys) { $System.out.println ("Name:" +entry.getkey () + "ID:" +Entry.getvalue ()); - } - } the}
Printing results:
Name: id:1101name:lisi ID:1104Name: Small two ID:1102Name: 3 ID:1103
Related methods:
Construction method: Properties ();//No default value
Properties (properties defaults);//Specify Default values
Configuration information: SetProperties (Key,value);//Key values are string types
Write Data:(1) Store (new FileWriter ("c:\\."), "description of configuration Information"); A character stream is recommended if you need to write to Chinese
(2) Store (new FileOutputStream (c:\\.) , "Description of the configuration Information"); BYTE stream
Read data: Load (new FileReader ("c:\\.")); Character Stream read
Simple use of the Java configuration information class Properties