標籤:localhost main leo bin ext int closed oca utf-8
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 { /** * @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); }}demo1
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;/** * 使用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配置"); }}demo2
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設定檔