Properties read the configuration file key and values are all string types
PackageCom.bjsxt.others.pro;Importjava.util.Properties;/*** Properties Resource configuration file read/write * 1, key and value can only be String * 2, store and read * SetProperty (string key, String value) * GetProperty (string Key, String DefaultValue) *@authorAdministrator **/ Public classDemo01 {/** * @paramargs*/ Public Static voidMain (string[] args) {//Creating ObjectsProperties Pro =NewProperties (); //StoragePro.setproperty ("Driver", "Oracle.jdbc.driver.OracleDriver"); //pro.setproperty ("url", "Jdbc:oracle:thin: @localhost: 1521:orcl");Pro.setproperty ("User", "Scott"); Pro.setproperty ("PWD", "Tiger"); //GetString url =pro.getproperty ("url", "Test"); System.out.println (URL); }}
Demo1
PackageCom.bjsxt.others.pro;ImportJava.io.File;Importjava.io.FileNotFoundException;ImportJava.io.FileOutputStream;Importjava.io.IOException;Importjava.util.Properties;/*** Output to file using Properties * Resource Profile: * * 1,. Properties * Store (OutputStream out, String comments) store (writer writer, String comments) 2,. xml Storetoxml (outputstream OS, string comment): UTF-8 character Set Storetoxml (OutputStream OS, string Comment, String encoding) *@authorAdministrator **/ Public classDemo02 {/** * @paramargs *@throwsIOException *@throwsFileNotFoundException*/ Public Static voidMain (string[] args)throwsFileNotFoundException, IOException {//Creating ObjectsProperties Pro =NewProperties (); //StoragePro.setproperty ("Driver", "Oracle.jdbc.driver.OracleDriver"); Pro.setproperty ("url", "Jdbc:oracle:thin: @localhost: 1521:orcl"); Pro.setproperty ("User", "Scott"); Pro.setproperty ("PWD", "Tiger"); //Save to E:/others absolute path drive letter://Pro.store (New FileOutputStream ("E:/others/db.properties"), "DB Configuration"); //Pro.storetoxml (New FileOutputStream ("E:/others/db.xml"), "DB Configuration"); //using relative paths for current projects//Pro.store (New FileOutputStream ("Db.properties"), "DB Configuration");//Pro.store (New FileOutputStream ("Src/db.properties"), "DB Configuration");Pro.store (NewFileOutputStream (NewFile ("Src/com/bjsxt/others/pro/db.properties"), "DB Configuration"); }}
Demo2
PackageCom.bjsxt.others.pro;Importjava.io.FileNotFoundException;ImportJava.io.FileReader;Importjava.io.IOException;Importjava.util.Properties;/*** Use properties to read configuration files * Resource profile: * Use relative to absolute path read * Load (InputStream instream) load (Reader Reader) LoadFromXML (Input Stream in) *@authorAdministrator **/ Public classDemo03 {/** * @paramargs *@throwsIOException *@throwsFileNotFoundException*/ Public Static voidMain (string[] args)throwsFileNotFoundException, IOException {Properties Pro=NewProperties (); //Read absolute path//Pro.load (New FileReader ("E:/others/db.properties")); //Read relative pathPro.load (NewFileReader ("Src/com/bjsxt/others/pro/db.properties")); System.out.println (Pro.getproperty ("User", "BJSXT")); }}
Demo3
PackageCom.bjsxt.others.pro;Importjava.io.IOException;Importjava.util.Properties;/*** Read the configuration file using the class-relative path * bin *@authorAdministrator **/ Public classDemo04 {/** * @paramargs *@throwsIOException*/ Public Static voidMain (string[] args)throwsIOException {Properties Pro=NewProperties (); //class-relative path/bin//Pro.load (Demo04.class.getResourceAsStream ("/com/bjsxt/others/pro/db.properties")); //"" BinPro.load (Thread.CurrentThread (). Getcontextclassloader (). getResourceAsStream ("com/bjsxt/others/pro/ Db.properties ")); System.out.println (Pro.getproperty ("User", "BJSXT")); }}
Demo4
The properties class in Java reads the K-V configuration file