读取根目录src下的指定配置properties文件内容
2013-11-21 14:38
453 查看
代码如下:
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(); } } }
相关文章推荐
- Java读取src目录下的properties配置文件
- 编写一个文件,读取 src 下面day12包 下面的my.properties 文件,文件内容如下, className=day12.User 结合 IO、反射知识,完成 配置文件中对象的创建和方法调用。并编写方法,列举出 Student 类中所有的属性、
- Maven项目中读取src/main/resources目录下的配置文件
- Java web 项目读取src或者tomcat下class目录下的xml文件或者properties文件
- java修改读取properties配置文件中的内容
- java 读取properties配置文件内容乱码 --日文乱码对应方法
- Java中读取txt文本中内容+目录文件复制到指定目录
- 读取properties文件的内容配置数据库
- python 学习记录(11)-文件处理/读取文件/文件写入内容/文件删除/文件复制/文件重命名/后缀名/内容查找与替换/文件比较/ 配置文件访问/目录创建与删除/遍历目录/定向输出
- Java读取指定目录下的文件内容
- 如何读取.properties配置文件的内容
- 使用python读取指定目录下的源代码并将内容复制到同一新文件中
- javaweb读取任意目录的下的properties配置文件(解决普通java类读web-inf下任意目录)
- Maven项目中读取src/main/resources目录下的配置文件
- 读取static 中读取SRC目录下的配置文件
- Java中读取properties属性配置文件内容方法
- 21.4 实例:读取目录下指定类型的文件的内容
- 指定文件名读取WEB-INFO目录下properties配置(windows平台)
- java.util.Properties读取中文内容的配置文件,发生中文乱码的现象有解决方案
- 读取 src 目录下 自定义配置文件