一个读取properties的工具类!请大家多多指教!
2012-11-27 15:50
197 查看
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
import org.apache.log4j.Logger;
/**
*
* Description:系统读取配置文件工具类
* All Rights Reserved.
* @version 1.0 2012-10-12 下午1:28:25 by albert(albertwxh@gmail.com)创建
*/
public class PropertiesUtil {
private static Logger logger = Logger.getLogger(PropertiesUtil.class);
public final static String CACHE_PROPERTIES_NAME = "cacheData";
/**
*
* Description:根据文件名称创建Properties对象
* @Version1.0 2012-10-12 下午1:31:30 by albert(albertwxh@gmail.com)创建
* @param propertiesName 资源文件名,推荐使用PropertiesUtil类的静态常量
* @return Properties对象
*/
public static Properties getProperties(String propertiesName){
String propertiesPath = getConfigPath(propertiesName);
return createProperties(propertiesPath);
}
/**
*
* Description: 获取Properties文件路径
* @Version1.0 2012-10-12 下午1:33:04 by albert(albertwxh@gmail.com)创建
* @param propertiesName 资源文件名,推荐使用PropertiesUtil类的静态常量
* @return 文件路径
*/
private static String getConfigPath(String propertiesName){
String resultPath = null ;
String classPath = PropertiesUtil.class.getClassLoader().getResource(PropertiesUtil.class.getName().replace('.', '/')+".class").getFile();
String classRoot = "";
classRoot = classPath.substring(0,classPath.indexOf("/com/common/util"));
if(!classRoot.equals("") && classRoot.startsWith("/") && classRoot.indexOf(":") == 2){
classRoot = classRoot.substring(1);
}
if(!classRoot.equals("")){
resultPath = classRoot+"/"+propertiesName+".properties";
}
return resultPath;
}
/**
*
* Description: 内部方法,根据Properties文件路径创建Properties对象
* @Version1.0 2012-10-12 下午1:40:55 by albert(albertwxh@gmail.com)创建
* @param fileFullName
* @return Properties对象
*/
private static Properties createProperties(String fileFullName){
try {
File file = new File(fileFullName);
FileInputStream fis = null ;
Properties properties = new Properties();
fis = new FileInputStream(file);
properties.load(fis);
return properties;
}catch (FileNotFoundException e) {
logger.error("系统读取Properties文件发生异常",e);
return null ;
}catch(IOException e){
logger.error("系统读取配置文件加载Properties对象时发生异常",e);
return null;
}
}
}
希望大家指教!
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
import org.apache.log4j.Logger;
/**
*
* Description:系统读取配置文件工具类
* All Rights Reserved.
* @version 1.0 2012-10-12 下午1:28:25 by albert(albertwxh@gmail.com)创建
*/
public class PropertiesUtil {
private static Logger logger = Logger.getLogger(PropertiesUtil.class);
public final static String CACHE_PROPERTIES_NAME = "cacheData";
/**
*
* Description:根据文件名称创建Properties对象
* @Version1.0 2012-10-12 下午1:31:30 by albert(albertwxh@gmail.com)创建
* @param propertiesName 资源文件名,推荐使用PropertiesUtil类的静态常量
* @return Properties对象
*/
public static Properties getProperties(String propertiesName){
String propertiesPath = getConfigPath(propertiesName);
return createProperties(propertiesPath);
}
/**
*
* Description: 获取Properties文件路径
* @Version1.0 2012-10-12 下午1:33:04 by albert(albertwxh@gmail.com)创建
* @param propertiesName 资源文件名,推荐使用PropertiesUtil类的静态常量
* @return 文件路径
*/
private static String getConfigPath(String propertiesName){
String resultPath = null ;
String classPath = PropertiesUtil.class.getClassLoader().getResource(PropertiesUtil.class.getName().replace('.', '/')+".class").getFile();
String classRoot = "";
classRoot = classPath.substring(0,classPath.indexOf("/com/common/util"));
if(!classRoot.equals("") && classRoot.startsWith("/") && classRoot.indexOf(":") == 2){
classRoot = classRoot.substring(1);
}
if(!classRoot.equals("")){
resultPath = classRoot+"/"+propertiesName+".properties";
}
return resultPath;
}
/**
*
* Description: 内部方法,根据Properties文件路径创建Properties对象
* @Version1.0 2012-10-12 下午1:40:55 by albert(albertwxh@gmail.com)创建
* @param fileFullName
* @return Properties对象
*/
private static Properties createProperties(String fileFullName){
try {
File file = new File(fileFullName);
FileInputStream fis = null ;
Properties properties = new Properties();
fis = new FileInputStream(file);
properties.load(fis);
return properties;
}catch (FileNotFoundException e) {
logger.error("系统读取Properties文件发生异常",e);
return null ;
}catch(IOException e){
logger.error("系统读取配置文件加载Properties对象时发生异常",e);
return null;
}
}
}
希望大家指教!
相关文章推荐
- 方法:一个简单的读取配置文件.properties的工具类 JAVA
- 一个读取配置文件的工具类写法 (properties文件和文本文件)
- 121 项目 018 笔记向 properties 读取工具类
- java 数据库读取工具类(读取config.properties配置文件)
- java读取properties配置文件工具类
- 发布一个文章读取模块,有前一篇和后一篇文章的功能,存储过程我直接贴上来了,大家自己改吧!
- Java属性Properties文件读取工具类
- java 数据库读取工具类(读取config.properties配置文件)
- JAVA工具类(9)--属性文件properties的读取
- (1) 写一个 Properties 格式的配置文件,配置类的完整名称。 * (2) 写一个程序,读取这个 Properties 配置文件,获得类的完整名称并加载这个类,用 反射 的方式运行
- 【表单验证】一个新手做的 大家多多指教
- ReadProcessMemory函数读取基址时输出却变成了一个地址,请教一下大家
- 一个常用工具类,读取配置文件
- weixin4j开发—为大家提供一个获取Weixin对象的工具类
- java 数据库读取工具类(读取config.properties配置文件)[包含线程安全]
- Java工具类--读取Properties文件
- 读取properties配置文件工具类
- Java properties 文件读取 工具类的封装
- 向大家推荐一个好用的 内存清理及内存工具类
- 读取Properties信息工具类