读取根目录src下的指定配置properties文件内容
代码如下:
package com.chen.system.util; import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties; import com.chen.system.constanct.GenerateConstancts; public class PropertiesUtil {
private static Properties properties = new Properties(); /**获取src下配置文件内容
*
* @author : 陈惟鲜
* @create_date :2013-11-21 下午2:34:44
* @param key
* @return
* @throws IOException
*/
public static synchronized String get(String key) throws IOException {
// ClassLoader cl = Thread.currentThread().getContextClassLoader();
if (properties.isEmpty())
{
// 获取编译后文件路径
String path = PropertiesUtil.class.getResource("/").toString().replace("file:/", "");
File rootFile = new File(path); // 读取路径下的文件
if (rootFile.isDirectory()) {
for (File file : rootFile.listFiles()) {
// 当文件名称为指定格式后,读取
if (file.getName().lastIndexOf(".properties") > 0) {
if (file.isFile()) {
InputStream is = new FileInputStream(file);
properties.load(is);
}
}
}
}
} return properties.getProperty(key);
} /**
* @author : 陈惟鲜
* @create_date :2013-11-20 下午3:22:25
* @param args
*/
public static void main(String[] args) {
// 获取配置文件信息
Properties properties = new Properties();
try {
System.out.println("结果:" + PropertiesUtil.get(GenerateConstancts.BEAN_NAME));
System.out.println("结果:" + PropertiesUtil.get("bb"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
读取根目录src下的指定配置properties文件内容的更多相关文章
- 五种方式让你在java中读取properties文件内容不再是难题
一.背景 最近,在项目开发的过程中,遇到需要在properties文件中定义一些自定义的变量,以供java程序动态的读取,修改变量,不再需要修改代码的问题.就借此机会把Spring+SpringMVC ...
- SpringMVC加载配置Properties文件的几种方式
最近开发的项目使用了SpringMVC的框架,用下来感觉SpringMVC的代码实现的非常优雅,功能也非常强大, 网上介绍Controller参数绑定.URL映射的文章都很多了,写这篇博客主要总结一下 ...
- 六种获取配置properties文件的方法
总结一下六种获取配置properties文件的方法,代码如下: package com.xujingyang.test ; import java.io.BufferedInputStream ; i ...
- linux下查找指定后缀的文件
1.linux下查找指定后缀的文件 例如查找当前目录下的所有后缀名时.c或.h的文件 find . -type f -regex ".*\.\(c\|h\)"
- Java中如何获取spring中配置的properties文件内容
有2种方式: 一. 1.通过spring配置properties文件 [java] <bean id="propertyConfigurer" class=&qu ...
- 解决IntelliJ IDEA无法读取配置*.properties文件的问题
idea对这些配置的文件方式很明显和eclipse是不同的.在idea中有一个 Content Roots的概念.需要为每一个folder配置相应的Content Roots.Content Root ...
- Spring 如何读取properties文件内容
http://hi.baidu.com/alizv/blog/item/d8cb2af4094662dbf3d38539.html 在现实工作中,我们常常需要保存一些系统配置信息,大家一般都会选择配置 ...
- Window Linux下实现指定目录内文件变更的监控方法
转自:http://qbaok.blog.163.com/blog/static/10129265201112302014782/ 对于监控指定目录内文件变更,window 系统提供了两个未公开API ...
- 分别用Java和JS读取Properties文件内容
项目中经常用到的配置文件,除了XML文件之外,还会用到Properties文件来存储一些信息,例如国际化的设置.jdbc连接信息的配置等.有时候也会把一些路径或者sql语句放到Properties中, ...
随机推荐
- Vue ---- Vuex 的第一次接触
在 Vue.js 的项目中,如果项目结构简单, 父子组件之间的数据传递可以使用 props 或者 $emit 等方式 http://www.cnblogs.com/wisewrong/p/62660 ...
- kafka producer partitions分区器(七)
消息在经过拦截器.序列化后,就需要确定它发往哪个分区,如果在ProducerRecord中指定了partition字段,那么就不再需要partitioner分区器进行分区了,如果没有指定,那么会根据k ...
- Android Studio彻底删除Module
在"Project"视图中选择需要删除的module名,此处删除"app",点击右键,选择"Open Module Setting",然后选 ...
- 星舟平台的使用(GIT、spring Boot 的使用以及swagger组件的使用)
一.介绍星舟平台 1.星舟简介 2.网关kong的介绍 3.客户端 1).服务注册:Eureka 2).客户端负载均衡:Ribbon 4 ...
- C库函数:scanf、fscanf、printf、fprintf、sprintf、 snprintf
1. scanf 函数原型 int scanf(const char *format, ...); 功能:从标准输入 stdin 读取格式化输入. 2.fscanf 函数原型 int fscanf( ...
- 关于@service、@controller和@transactional 在spring中的位置说明
Spring容器优先加载由ServletContextListener(对应applicationContext.xml)产生的父容器,而SpringMVC(对应mvc_dispatcher_serv ...
- JavaScript数组方法之reduce
又见到数组方法了,在前面已经的多次写到过数组方法,甚至都使用原生方法重构了一遍数组的各个方法,可是随着数组方法reduce的应用,发现reduce真的是妙用无穷啊!还是很值得再拿出来说一遍的. 我们再 ...
- Spring HttpServletRequest对象的获取
1.Controller方法上获取 @RequestMapping(value = "/aliyun/ccc/callComing", method = RequestMethod ...
- springboot+mybatis调用oracle存储过程
1 存储过程参数为VARCHAR 代码逻辑:controller层定义实体类对象entity,并entity.set给存储过程的输入参数赋值,把赋值后的实体类通过service层传到dao层,然后通过 ...
- X509证书在window server 2003/IIS 6环境部署
利用makecert.exe工具生成的X509证书在winform程序中运行正常,但是给部署在IIS中的应用程序用却获取不到证书信息,返回为空.原因是,iis没有权限读取位于证书存储区的X509证书, ...