首页 > 编程语言 > 详细

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

时间:2017-08-07 09:40:03      阅读:440      评论:0      收藏:0      [点我收藏+]
通过读流的方式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包外部的配置文件

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!