A property is a form that appears frequently in a program.
A specialized properties class is provided in the class set.
Public class Propertiesextends hashtable<object,object>
Properties is a Hashtable subclass, then it is definitely a map subclass. You can use all of the map's operations.
However, it is generally used alone.
Setting and Getting properties
Set properties.
Object SetProperty (string key, String value)
Get Properties:
The return value was found and no return null was found.
String GetProperty (String key)
and found the return value, did not find the return defaultvalue default value.
DefaultValue)
Verify the above action method:
Packageclass set;Importjava.util.Properties; Public classtest1{ Public Static voidMain (String args[]) { Properties pro = new Properties (); Create a Properties ObjectPro.SetProperty("BJ", "Beijing");//Setting PropertiesPro.SetProperty("TJ", "Tianjin") ; Pro. SetProperty ("NJ", "Nanjing") ; System.out.println ("1, BJ Attribute exists:" + Pro.GetProperty("BJ")) ; System.out.println ("2, SC attribute does not exist:" + Pro.GetProperty("SC")) ; System.out.println ("3, the SC attribute does not exist, and the default value of the display is set:" + Pro.GetProperty("SC", "No Discovery")) ; Failed to find return default value}};
Results:
1, BJ attribute exists: Beijing2, the SC attribute does not exist:null3, the SC attribute does not exist, and the default value of the display is set: no discovery
Write the above property toNormalFile, the byte stream operation.
void Store (outputstream ouTString comments)
Code:
Packageclass set;ImportJava.io.File;Importjava.io.FileNotFoundException;ImportJava.io.FileOutputStream;Importjava.io.IOException;Importjava.util.Properties; Public classtest1{ Public Static voidMain (String args[]) { Properties pro = new Properties (); Create Properties Object Pro.setproperty ("BJ", "Beijing"); Setting PropertiesPro.setproperty ("TJ", "Tianjin") ; Pro.setproperty ("NJ", "Nanjing") ; File File = new File ("D:" + file.separator + "Area.properteis"); Specify the files to manipulate Try{Pro. Store(new FileOutputStream (file), "area Info");//save properties to normal file}Catch(filenotfoundexception e) { //exception handling. E.printstacktrace (); }Catch(ioexception e) {e.printstacktrace (); } }};
Results:
be aware of the exception handling for files .
Now that you can save toNormalFile, you can read the file.
void Load (InputStream instream)
Code to read the contents of a property in a file:
Packageclass set;ImportJava.io.File;ImportJava.io.FileInputStream;Importjava.io.FileNotFoundException;Importjava.io.IOException;Importjava.util.Properties; Public classtest1{ Public Static voidMain (String args[]) { Properties pro = new Properties (); //Create a Properties object File File = new file ("D:" + file.separator + "Area.properteis"); Specify the files to manipulate Try{Pro. Load(new FileInputStream (file)) ;//Read Properties File}Catch(FileNotFoundException e) {e.printstacktrace (); }Catch(IOException e) {e.printstacktrace (); } System.out.println ("1, BJ Attribute exists:" + Pro.GetProperty("BJ")) ; System.out.println ("2, SH attribute exists:" + Pro.GetProperty("SH")) ; }};
Output Result:
1, BJ attribute exists: Beijing2, sh attribute exists:null
Saved to an XML file.
void storetoxml(outputstream os, String comment)
Code:
Packageclass set;ImportJava.io.File;ImportJava.io.FileInputStream;Importjava.io.FileNotFoundException;ImportJava.io.FileOutputStream;Importjava.io.IOException;Importjava.util.Properties; Public classtest1{ Public Static voidMain (String args[]) {Properties Pro=NewProperties ();//Create a Properties objectPro.setproperty ("BJ", "Beijing");//Setting PropertiesPro.setproperty ("TJ", "Tianjin") ; Pro.setproperty ("NJ", "Nanjing") ; File File = new File ("D:" + file.separator + "area. XML") ;//specify the files to manipulate Try{Pro. Storetoxml(new FileOutputStream (file), "area Info") ;//save properties to normal file}Catch(FileNotFoundException e) {e.printstacktrace (); }Catch(IOException e) {e.printstacktrace (); } }};
Results:
Read properties in XML file
void LoadFromXML (InputStream in)
Code:
Packageclass set;ImportJava.io.File;ImportJava.io.FileInputStream;Importjava.io.FileNotFoundException;Importjava.io.IOException;Importjava.util.Properties; Public classtest1{ Public Static voidMain (String args[]) {Properties Pro=NewProperties ();//Create a Properties objectFile File =NewFile ("D:" + file.separator + "Area.xml");//specify the files to manipulate Try{pro.loadfromxml (NewFileInputStream (file));//Read Properties File}Catch(FileNotFoundException e) {e.printstacktrace (); }Catch(IOException e) {e.printstacktrace (); } System.out.println ("1, BJ Attribute exists:" + pro.getproperty ("BJ"))) ; }};
Operation Result:
1, BJ attribute exists: Beijing
Summarize:
1, to further understand the properties, you can learn the following reflection mechanism. Understanding attribute Applications
2, the type in the attribute must be a string. Because the operation is most convenient.
3, properties can be saved or read to a normal file or XML file, in the specified format to the file arbitrarily expanded properties.
Attribute class: Properties