The properties class in Java reads the K-V configuration file

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.