Java類訪問配置檔案
阿新 • • 發佈:2018-11-02
package com.dashuo.da.common.function; import java.io.FileInputStream; import java.util.Properties; public class MongoTask2 { /** * * 主測試方法 * */ public static void main(String[] args) { MongoTask2 jdbc = new MongoTask2(); jdbc.getParameter(); } private static Properties dbconfig = new Properties(); private String tbl_project_data = null; private String tbl_create_time = null; static { try { // 方式一: /*InputStream in = MongoTask2.class.getClassLoader().getResourceAsStream("value.properties"); dbconfig.load(in);*/ // 方式二 String realPath = MongoTask2.class.getClassLoader().getResource("value.properties").getPath(); FileInputStream in = new FileInputStream(realPath); dbconfig.load(in); } catch (Exception e) { e.printStackTrace(); } } /** * * 獲取配置檔案db.properties中連線資料庫的引數 * */ public void getParameter() { tbl_project_data = dbconfig.getProperty("ProjectData"); tbl_create_time = dbconfig.getProperty("CreateTimeData"); System.out.println(" MongoDB資料表:" + tbl_project_data + " MongoDB資料建立時間表:" + tbl_create_time ); } }