java类读取其所在jar包外部的配置文件

Posted dali_lyc

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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;
  }

}

 

以上是关于java类读取其所在jar包外部的配置文件的主要内容,如果未能解决你的问题,请参考以下文章