I. Propertie
It is mainly used for reading and writing resource configuration files.
Properties inherit from Hashtable.
Let's look at the difference between HashMap and Hashtable:
Hashtabl is thread-safe, synchronous and inefficient.
HashMap threads are insecure, asynchronous and efficient.
Neither Hashtable key nor value can be empty
HashMap allows one key to be null and multiple value s to be null.
Each key and value in Properties can only be a string, because it inherits from Hashtable, it can not be empty.
NullPointException is thrown for null.
Common methods
getProperty(String key)
Returns the corresponding value according to the key.
getProperty(String key, String defaultValue)
Returns the corresponding value according to the key, and defaultValue if it does not exist.
defaultValue is specified by itself as an alternate option when the key is empty.
setProperty(String key, String value)
Setting the key and value of Properties
Let's start with these methods, which are similar to Map.
import java.util.Iterator; import java.util.Map; import java.util.Map.Entry; import java.util.Properties; import java.util.Set; public class Test { public static void main(String args[]){ Properties pro = new Properties(); pro.setProperty("DIVER", "com.mysql.cj.jdbc.Driver");//Adding elements 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()); } } }
Operation results: key:PASS-->value:xxxxxx key:URL-->value:jdbc:mysql://localhost:3306/jdbc key:USER-->value:root key:DIVER-->value:com.mysql.cj.jdbc.Driver
At this point, the configuration information is in the pro object. Next, we will generate the configuration file with the configuration information.
Generate *. properties file
store(OutputStream out,String comments);
Generate *. xml file
storeToXml(OutputStream out,String comments);
import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.Iterator; import java.util.Map; import java.util.Map.Entry; import java.util.Properties; import java.util.Set; public class Test { public static void main(String args[]) throws FileNotFoundException, IOException{ Properties pro = new Properties(); 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(new FileOutputStream("E:/sqlProperties.properties"), "sql");//Generating files in properties format under E-disk pro.storeToXML(new FileOutputStream("E:/sqlXml.xml"), "sql");//Generating xml format file under E disk } }
After running, we will find two more files on disk E:
We use Notepad to open these two files:
Our previous configuration information generated the corresponding configuration file. -
Above is the absolute path, we generally use the relative path, default in the current project.
import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.Iterator; import java.util.Map; import java.util.Map.Entry; import java.util.Properties; import java.util.Set; public class Test { public static void main(String args[]) throws FileNotFoundException, IOException{ Properties pro = new Properties(); 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(new FileOutputStream("sqlProperties.properties"), "sql"); pro.storeToXML(new FileOutputStream("src/sqlXml.xml"), "sql"); } }
You can see that
"sqlProperties.properties" is direct