java 中properties 類讀取k-v配置文件
阿新 • • 發佈:2017-10-14
.class void tput iter 讀取配置文件 絕對路徑 getprop stream 源配置
properties 讀取的配置文件key和values都是string 類型
package com.bjsxt.others.pro; import java.util.Properties; /** * Properties 資源配置文件的讀寫 * 1、key 與value 只能為字符串 * 2、存儲與讀取 * setProperty(String key, String value) * getProperty(String key, String defaultValue) * @author Administrator * */ public class Demo01 {demo1/** * @param args */ public static void main(String[] args) { //創建對象 Properties pro =new Properties(); //存儲 pro.setProperty("driver", "oracle.jdbc.driver.OracleDriver"); //pro.setProperty("url", "jdbc:oracle:thin:@localhost:1521:orcl"); pro.setProperty("user", "scott"); pro.setProperty("pwd", "tiger"); //獲取 String url =pro.getProperty("url","test"); System.out.println(url); } }
package com.bjsxt.others.pro; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.Properties;demo2/** * 使用Properties 輸出到文件 * 資源配置文件: * * 1、.properties * store(OutputStream out, String comments) store(Writer writer, String comments) 2、.xml storeToXML(OutputStream os, String comment) :UTF-8字符集 storeToXML(OutputStream os, String comment, String encoding) * @author Administrator * */ public class Demo02 { /** * @param args * @throws IOException * @throws FileNotFoundException */ public static void main(String[] args) throws FileNotFoundException, IOException { //創建對象 Properties pro =new Properties(); //存儲 pro.setProperty("driver", "oracle.jdbc.driver.OracleDriver"); pro.setProperty("url", "jdbc:oracle:thin:@localhost:1521:orcl"); pro.setProperty("user", "scott"); pro.setProperty("pwd", "tiger"); //存儲到e:/others 絕對路徑 盤符: //pro.store(new FileOutputStream(new File("e:/others/db.properties")), "db配置"); //pro.storeToXML(new FileOutputStream(new File("e:/others/db.xml")), "db配置"); //使用相對路徑 當前的工程 // pro.store(new FileOutputStream(new File("db.properties")), "db配置"); // pro.store(new FileOutputStream(new File("src/db.properties")), "db配置"); pro.store(new FileOutputStream(new File("src/com/bjsxt/others/pro/db.properties")), "db配置"); } }
package com.bjsxt.others.pro; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.util.Properties; /** * 使用Properties讀取配置文件 * 資源配置文件: * 使用相對與絕對路徑讀取 * load(InputStream inStream) load(Reader reader) loadFromXML(InputStream in) * @author Administrator * */ public class Demo03 { /** * @param args * @throws IOException * @throws FileNotFoundException */ public static void main(String[] args) throws FileNotFoundException, IOException { Properties pro=new Properties(); //讀取 絕對路徑 //pro.load(new FileReader("e:/others/db.properties")); //讀取 相對路徑 pro.load(new FileReader("src/com/bjsxt/others/pro/db.properties")); System.out.println(pro.getProperty("user", "bjsxt")); } }demo3
package com.bjsxt.others.pro; import java.io.IOException; import java.util.Properties; /** * 使用類相對路徑讀取配置文件 * bin * @author Administrator * */ public class Demo04 { /** * @param args * @throws IOException */ public static void main(String[] args) throws IOException { Properties pro =new Properties(); //類相對路徑的 / bin //pro.load(Demo04.class.getResourceAsStream("/com/bjsxt/others/pro/db.properties")); //"" bin pro.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("com/bjsxt/others/pro/db.properties")); System.out.println(pro.getProperty("user", "bjsxt")); } }demo4
java 中properties 類讀取k-v配置文件