读取根目录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中, ...
随机推荐
- 使用JQuery获取被选中的checkbox的value值
上网查了一下,感觉一些人回答得真的是不知所云,要么代码不够简便.或者是有些想装逼成分等. 以下为使用JQuery获取input checkbox被选中的值代码: <html> & ...
- The Maze II
There is a ball in a maze with empty spaces and walls. The ball can go through empty spaces by rolli ...
- python3连接oracle数据库
声明:python,cx_Oracle和instantclient的版本应一致 我这里使用的版本是python3.6 64位 ,cx_Oracle-5.3-11g.win-amd64-py3.6-2和 ...
- 【AtCoder】AGC010
AGC010 A - Addition 如果所有数加起来是偶数那么一定可以,否则不行 #include <bits/stdc++.h> #define fi first #define s ...
- Linux系列(14)之工作管理
1.工作管理 说明:工作管理(job control)是用在bash环境下的,也就是说:“当我们登录系统取得bash shell之后,在单一终端机接口下同时进行多个工作的行为管理”.举例说明,我们在登 ...
- 创建Vofuria工程,获取产品密钥
进入Vofuria官网 https://developer.vuforia.com/vui/develop/licenses/free/new 然后点击 然后在License Name中填写izji刚 ...
- linux实现一个定时任务
设置定时任务删除logs脚本数据 编写脚本 touch cleanLogs.sh #! /bin/sh -name "*.log*" -exec rm -f {} \; 使用r ...
- hdu 3364 高斯入门。。
扣了一个高斯的介绍 比较全面(来自http://blog.csdn.net/duanxian0621/article/details/7408887) 高斯消元法,是线性代数中的一个算法,可用来求解线 ...
- CentOS7 mysql支持中文
# vim /etc/my.cnf # For advice on how to change settings please see# http://dev.mysql.com/doc/refman ...
- div可以同时设置背景图片和背景颜色吗?
前言 当然可以同时设置 当图片背景色不透明时 情况一:当图片的长.宽 >= div的长.宽时 我们最终看到div背景是图片,之所以说是最终看到,是因为在页面加载时,我们先看到的div背景是颜色, ...