First, Propertie
Mainly used for reading and writing resource configuration files.
Properties inherit from Hashtable.
Let's look at the difference between HashMap and Hashtable:
HASHTABL thread safety, synchronization, low efficiency.
HashMap threads are unsafe, unsynchronized, and highly efficient.
Hashtable key and value cannot be empty
HashMap allows a key to be null, allowing multiple value to be null.
Each key and value in properties can only be a string, and cannot be empty because it inherits from Hashtable.
Empty will throw nullpointexception.
Second, common methods
GetProperty (String key)
Returns the corresponding value according to key.
GetProperty (string key, String defaultvalue)
Returns the corresponding value according to key, or returns DefaultValue if it does not exist.
DefaultValue itself, as a fallback option when key is empty.
SetProperty (string key, String value)
Set the properties key and value
Let's start with these methods, which are similar to the previous use of map
ImportJava.util.Iterator;ImportJava.util.Map;ImportJava.util.Map.Entry;Importjava.util.Properties;ImportJava.util.Set; Public classTest { Public Static voidMain (String args[]) {Properties Pro=NewProperties (); Pro.setproperty ("DIVER", "Com.mysql.cj.jdbc.Driver");//add Element Pro.setproperty ("URL", "JDBC:MYSQL://LOCALHOST:3306/JDBC"); Pro.setproperty ("USER", "root"); Pro.setproperty ("PASS", "xxxxxx"); Set<map.entry<object, object>> ms=Pro.entryset ();//use iterators to output all elements Iterator<Map.Entry<Object,Object>> ite =Ms.iterator (); while(Ite.hasnext ()) {Entry<Object,Object> en =Ite.next (); System.out.println ("Key:" +en.getkey () + "+" + "value:" +En.getvalue ()); } }}
running Result: Key:pass--Value:xxxxxxkey:URL-->value:jdbc:mysql://localhost:3306/ JDBCkey:user-->Value:rootkey:DIVER-->value:com.mysql.cj.jdbc.driver
These configuration information is now available in the Pro object, and we will then generate the configuration files for these configuration information.
Generate *.properties File
Store (OutputStream out,string comments);
Generate *.xml File
Storetoxml (OutputStream out,string comments);
Importjava.io.FileNotFoundException;ImportJava.io.FileOutputStream;Importjava.io.IOException;ImportJava.util.Iterator;ImportJava.util.Map;ImportJava.util.Map.Entry;Importjava.util.Properties;ImportJava.util.Set; Public classTest { Public Static voidMain (String args[])throwsFileNotFoundException, ioexception{Properties Pro=NewProperties (); Pro.setproperty ("DIVER", "Com.mysql.cj.jdbc.Driver"); Pro.setproperty ("URL", "JDBC:MYSQL://LOCALHOST:3306/JDBC"); Pro.setproperty ("USER", "root"); Pro.setproperty ("PASS", "xxxxxx"); Pro.store (NewFileOutputStream ("E:/sqlproperties.properties"), "SQL");//e disk generates a properties format file Pro.storetoxml (NewFileOutputStream ("E:/sqlxml.xml"), "SQL"); Generate XML format file under//e disk}}
After the run we will find two more files on the E drive:
We open these two files with Notepad:
Our previous configuration information generated the corresponding configuration file.
The above is used absolute path, we generally use relative path, default in the current project.
Importjava.io.FileNotFoundException;ImportJava.io.FileOutputStream;Importjava.io.IOException;ImportJava.util.Iterator;ImportJava.util.Map;ImportJava.util.Map.Entry;Importjava.util.Properties;ImportJava.util.Set; Public classTest { Public Static voidMain (String args[])throwsFileNotFoundException, ioexception{Properties Pro=NewProperties (); Pro.setproperty ("DIVER", "Com.mysql.cj.jdbc.Driver"); Pro.setproperty ("URL", "JDBC:MYSQL://LOCALHOST:3306/JDBC"); Pro.setproperty ("USER", "root"); Pro.setproperty ("PASS", "xxxxxx"); Pro.store (NewFileOutputStream ("Sqlproperties.properties"), "SQL"); Pro.storetoxml (NewFileOutputStream ("Src/sqlxml.xml"), "SQL"); }}
Can see
"Sqlproperties.properties" is saved directly under the current project,
"Src/sqlxml.xml" is saved under the current project under the SRC folder.
If the absolute path is not used, the front-end defaults to the current project path.
This is the relative path relative to the project.
5.8 Properties