码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2017-08-07 10:21:48      阅读:205      评论:0      收藏:0      [点我收藏+]

标签:静态块   source   exception   catch   private   ops   cep   accesskey   执行   

通过读流的方式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包外部的配置文件

标签:静态块   source   exception   catch   private   ops   cep   accesskey   执行   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!