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