java類讀取其所在jar包外部的配置文件


通過讀流的方式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;
  }

}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM