JAVA 讀取配置檔案的實現類
阿新 • • 發佈:2019-08-20
1. 配置檔案 myconf.properties
url=www.baidu.com
2. 類檔案 MyProperties.class
public class MyConf { public static Properties properties = new Properties(); public static String url= getPropertieVal("url"); public static String getPropertieVal(String propertype){ if(properties==null || properties.size()==0) readProperties(); return properties.getProperty(propertype); } public static void readProperties() { try { properties.load(MyConf.class.getResourceAsStream("/myconf.properties")); }catch(FileNotFoundException e){ System.out.println("Error read"); } catch (IOException e) { e.printStackTrace(); } } }
3. 呼叫
Class Test {
public static void main(String[] args) {
System.out.println(MyConf.url);
}
}