properties资源文件是放在resource目录下的:

  

新建工具类:

package com.demo.utils;

import java.io.InputStream;
import java.util.Properties; public class PropertyUtil { /**
* 解析properties文件。
*
* @param path:properties文件的路径
* @param key: 获取对应key的属性
* @return String:返回对应key的属性,失败时候为null。
*/
public static String getPropertyByKey(String path, String key) throws Exception {
String result = null; InputStream is = PropertyUtil.class.getClassLoader().getResourceAsStream(path);
Properties p = new Properties();
p.load(is);
result = p.getProperty(key);
return result;
} public static void main(String[] args) {
String url = "";
try {
url = PropertyUtil.getPropertyByKey("api.properties", "dc_url");
} catch (Exception e) {
e.printStackTrace();
//单文件运行测试时是获取不到资源文件的,需在项目中
System.out.println("出错啦");
}
System.out.println(url);
} }

实际项目中引用时,如下:

     String url = "";
try {
url = PropertyUtil.getPropertyByKey("api.properties", "dc_url");
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("从资源文件获取url时出错", e);
}

放在其他目录下的资源文件可以参考:https://www.cnblogs.com/yanwenxiong/p/5595255.html

最新文章

  1. JDK1.7.0_45源码阅读<java.lang.Boolean>
  2. Solr atomic update JSON方式
  3. Java数据类型转换
  4. c#检测端口是否被占用
  5. opencv学习笔记(六)直方图比较图片相似度
  6. spring+mybatis事务管理
  7. BZOJ3996 [TJOI2015]线性代数
  8. 【现代程序设计】【期末作业】【homework-09】
  9. JSP JSP工作原理 JSP语法 JSP声明 JSP注释 JSP指令 jsp九大隐式/内置对象
  10. Prefixes and Suffixes
  11. C#深复制与浅复制
  12. 记一次被yield return坑的历程。
  13. python中logging模块的用法
  14. vetur插件提示 [vue-language-server] Elements in iteration expect to have 'v-bind:key' directives错误的解决办法
  15. xslfo和fop使用中的一些问题
  16. java学习笔记(一):开始第一个java项目
  17. 《SQL Server 2008从入门到精通》--20180716
  18. linux shell脚本编程笔记(四): 获取字符串长度的七种方法
  19. ZSTU4266 回文 2017-03-22 14:25 55人阅读 评论(0) 收藏
  20. 数据分析与展示---Numpy入门

热门文章

  1. Cannot retrieve metalink for repository: epel/x86_64. Please verify its path and try again
  2. Java和C++的数组比较
  3. WebApi 增加身份验证 (OAuth 2.0方式)
  4. 实验十一 团队作业7---团队项目设计完善&编码测试
  5. python3 tkinter添加图片和文本
  6. Jmeter 聚合报告---测试结果分析
  7. scrum与第一次teamwork
  8. Python的条件判断语句------if/else语句
  9. shell脚本学习-循环
  10. 流量控制与RateLimiter