Introduction to the 1.Properties class
Properties Class (Java.util.Properties), mainly used to read Java configuration files, various languages have their own supported configuration files, many variables in the configuration file is often changed, and this is to facilitate the user, so that users can be removed from the program itself to modify the relevant variable settings. Like a python-supported profile is an. ini file, it also has its own class configparse that reads the configuration file, allowing programmers or users to modify the. ini configuration file by means of this class. In Java, its configuration file is usually a. properties file, formatted as a text file, the format of the contents of the file is "key = value" format, the text annotation information can be used "#" to comment.
|
#person Information
#Sat Dec 15:00:22 CST 2017
Age=18
Name=zhangsan |
the parent class of the properties is the HashTable class.
|
Common methods of the Propertise class:
(1) 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.
(2) GetProperty (String key): 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.
(3) Load (InputStream instream): Reads the list of attributes (key and element pairs) from the input stream.
(4) Store (OutputStream out, String comments): Writes the attribute list (key and element pairs) in the properties table to the output stream. Comments is the annotation parameter. This method writes a key-value pair to the specified file.
(5) List (System.out): Print profile information to the console.
(6) Clear () to clear all loaded key-value pairs. This method is provided in the base class.
|
|
2.properties Method Demo
1 Public classPropertiesdemo {2 3 /** 4 * Properties commonly used in configuration file parameters 5 * @param args 6 */7 Public Static voidMain (string[] args) {8 //TODO auto-generated method stub9 Ten //methoddemo (); One //methoddemo2 (); AMethodDemo3 (); -} - the Private Static voidMethodDemo3 () { - //TODO auto-generated method stub -Properties prop =NewProperties (); - + //Create an input RU stream object -FileInputStream instream =NULL; + Try{ AInstream =NewFileInputStream ("h:\\workspace\\testfile\\info.properties"); atProp.load (instream);//Load Data - -Prop.setproperty ("name", "Zhangsan");//Modify Parameters - -Prop.list (System.out);//Print - in -}Catch(IOException e) { to //TODO auto-generated catch block +E.printstacktrace (); -}finally{ the if(Instream! =NULL){ * Try{ $Instream.close ();Panax Notoginseng}Catch(IOException e) { - //TODO auto-generated catch block the Throw NewRuntimeException (); +} A} the} + - $} $ - /** * Demo Properties Persistence * * - Private Static voidMethodDemo2 () {Wuyi //TODO auto-generated method stub theProperties prop =NewProperties (); - WuProp.setproperty ("name", "Zhangsan"); -Prop.setproperty (" Age", " -"); About $ //Create an output stream object -FileOutputStream fos =NULL; - Try{ -FOS =NewFileOutputStream ("h:\\workspace\\testfile\\info.properties"); AProp.store (FOS, "Person Information");//The second parameter is a comment message + the - $}Catch(IOException e) { the //TODO auto-generated catch block theE.printstacktrace (); the}finally{ the if(Fos! =NULL){ - Try{ inFos.close (); the}Catch(IOException e) { the //TODO auto-generated catch block About Throw NewRuntimeException (); the} the} the} + -} the Bayi /** * Demo setproperty () and Prop.list (system.out) * * - Private Static voidMethoddemo () { - //TODO auto-generated method stub theProperties prop =NewProperties (); the theProp.setproperty ("name", "Zhangsan"); theProp.setproperty (" Age", " -"); - theProp.list (System.out);//Print output directly using the list method the the /* 94 set<string> Set = Prop.stringpropertynames (); key:set for (string) {$ string value = Prop.getproperty (key); 98 System.out. println (key+ ":" +value); }*/ - 101}102 103}
2017-12-30 content from the Creative podcast program
Operation of the properties class in Java-Bakari-Blog Park
Java I/O---properties class