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;
  }

}
原文地址:https://www.cnblogs.com/dali-lyc/p/7297422.html