通過讀流的方式getResourceAsStream();
設置靜態塊,對象裝載時只執行一次;
提供get方法來訪問配置對象
public class FileConfig { private static String ACCESS_KEY;private static String END_POINT;static { Properties props = new Properties(); try { props.load(FileConfig.class.getResourceAsStream("/resource.properties")); ACCESS_KEY = props.getProperty("accessKey"); END_POINT = props.getProperty("endPoint"); } catch (Exception e) { System.out.println("can not find resource.properties file"); } } public static String getAccessKey() { return ACCESS_KEY; } public static String getEndPoint() { return END_POINT; } }