Java Application下讀取properties配置文
在java應用程序開發中,經常需要讀取配置文件來獲取系統參數或配置信息。配置文件可以使用xml格式文件,在java中存在.properties文件專門用作配置文件使用。在java中,類Properties用于處理配置文件相關的讀取。下面是一個關于根據所提供的鍵獲取值的示例。
public static String getvalue(String key) { Properties p=new Properties(); FileInputStream fis; String url = new File("").getAbsolutePath() + File.separator+ "config.properties"; // 獲取位于工程根目錄下的config.properties配置文件絕對路徑 try { fis = new FileInputStream(url); p.load(fis); fis.close(); } catch (Exception e) { // TODO Auto-generated catch block System.out.print("problems with properties"); e.printStackTrace(); } return p.getProperty(key); } |