java中根据key获取resource下properties资源文件中对应的参数
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
java中根据key获取resource下properties资源文件中对应的参数的更多相关文章
- maven 打包时动态替换properties资源文件中的配置值
pom build节点下面添加resource配置: <resources> <resource> <directory>src/main/resources/&l ...
- Direct2D开发:MFC下从资源文件中加载位图
转载请注明出处:http://www.cnblogs.com/ye-ming 0X01 概述: 相对于GDI处理界面,Direct2D有得天独厚的优势,下图就是Direct2D与GDI的效果对比,wi ...
- 在服务端中,读取properties资源文件中的数据
1.获取到资源的路径 2.读取数据 //properties文件对象 Properties properties = new Properties(); //通过HttpServletRequest ...
- vs下取得资源文件中的版本信息
在Windows Mobile和Wince(Windows Embedded CE)下开发的产品,有时候需要显示当前产品的版本信息.一般来说,版本信息是保存在资源文件里面的,例如下图: 为了保持一致, ...
- Windows Store App 全球化:引用分离资源文件中的资源
大部分应用程序仅需要单个默认资源文件,例如Strings/zh-CN/Resources.resw,但是在某些应用程序中,最好将资源分离到多个资源文件中,以便更好地组织资源内容,这样就需要考虑如何引用 ...
- “全栈2019”Java第十六章:下划线在数字中的意义
难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java第 ...
- Java - 得到项目中properties属性文件中定义的属性值
public static String getPropertiesValue(String fileName, String key) { return ResourceBundle.getBu ...
- nested exception is java.io.FileNotFoundException: class path resource [jdbc.properties] cannot be opened because it does not exist
Could not load properties; nested exception is java.io.FileNotFoundException: class path resource [j ...
- 装载Properties资源文件的项目中使用
ssm项目中打算将发短信的每小时每天的限定变成可配置的.于是将配置信息写在资源文件中,现在有两种方式加载资源文件,一个是使用spring注入方式,@Value注解注入,当然,前面需要在项目中装载.第二 ...
随机推荐
- new delate he typedef的含义
new: new 类型[初值] 如: new int ; //开辟一个存放整数的存储空间,返回一个指向该存储空间的 ...
- linux中du与df的区别和联系
1,两者区别 du,disk usage,是通过搜索文件来计算每个文件的大小然后累加,du能看到的文件只是一些当前存在 的,没有被删除的.他计算的大小就是当前他认为存在的所有文件大小的累加和. df, ...
- Servlet获取 URL 地址
使用 ServletRequest 的如下方法 getContextPath 取得项目名 getServletPath 取得Servlet名 getPathInfo 取得Servlet后的URL名,不 ...
- Java 中12个原子操作类
从JDK1.5 开始提供了 java.util.concurrent.atomic 包,该包提供了一种用法简单.性能高效.线程安全的更新一个变量的方法 原子更新基本类型类 AtomicBoolean: ...
- idea debug快捷键 快速查找类
快速查找类或者文件比如xml .txt Ctrl + Shift + N 快速查找类 双击Shift 选中代码右移 Tab 选中代码左移 Shift + Tab 选中代码上下移 Shift + Alt ...
- [Java]异常在项目中的使用
自己经历过的两个项目都有自定义异常,网上找了项目中自定义异常的例子: https://blog.csdn.net/aiyaya_/article/details/78989226. 这个例子基本上来说 ...
- squid故障汇总
1.COSS will not function without large file support (off_t is 4 bytes long. Please reconsider recomp ...
- 使用 Chrome 浏览器插件 Web Scraper 10分钟轻松实现网页数据的爬取
web scraper 下载:Web-Scraper_v0.2.0.10 使用 Chrome 浏览器插件 Web Scraper 可以轻松实现网页数据的爬取,不写代码,鼠标操作,点哪爬哪,还不用考虑爬 ...
- WindowsPE权威指南 第二章 小工具 pedump代码的C语言实现
2016-11-16 16:29:07 主程序代码 pedump.c #include <windows.h> #include <Richedit.h> #include & ...
- vue中引入vux
1.安装相关依赖 cnpm install vux --save cnpm install vux-loader --save-dev cnpm install less less-loader -- ...