package cn.dick.util;
import java.io.File;
import java.io.FileInputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Properties;
/**
* 动态读取配置文件类
* @author dick_wyq
* @version 1.0
*
*/
public class ConfigurationRead {
/**
* 属性文件全名
*/
private static final String PFILE = "config.properties";
/**
* 配置文件路径
*/
private URI uri = null;
/**
* 属性文件所对应的属性对象变量
*/
private long m_lastModifiedTime = 0;
/**
* 对应于属性文件的文件对象变量
*/
private File m_file = null;
/**
* 属性文件所对应的属性对象变量
*/
private Properties m_props = null;
/**
* 唯一实例
*/
private static ConfigurationRead m_instance = new ConfigurationRead();
/**
* 私有构造函数
* @throws URISyntaxException
*/
private ConfigurationRead() {
try {
m_lastModifiedTime = getFile().lastModified();
if (m_lastModifiedTime == 0) {
System.err.println(PFILE + "file does not exist!");
}
m_props = new Properties();
m_props.load(new FileInputStream(getFile()));
} catch (URISyntaxException e) {
System.err.println("文件路径不正确");
e.printStackTrace();
} catch (Exception e) {
System.err.println("文件读取异常");
e.printStackTrace();
}
}
/**
* 查找ClassPath路径获取文件
* @return File对象
* @throws URISyntaxException
*/
private File getFile() throws URISyntaxException {
URI fileUri = this.getClass().getClassLoader().getResource(PFILE)
.toURI();
m_file = new File(fileUri);
return m_file;
}
/**
* 静态工厂方法
* @return 返回ConfigurationRead的单一实例
*/
public synchronized static ConfigurationRead getInstance() {
return m_instance;
}
/**
* 读取一特定的属性项
*/
public String getConfigItem(String name, String defaultVal) {
long newTime = m_file.lastModified();
//检查属性文件是否被修改
if (newTime == 0) {
// 属性文件不存在
if (m_lastModifiedTime == 0) {
System.err.println(PFILE + " file does not exist!");
} else {
System.err.println(PFILE + " file was deleted!!");
}
return defaultVal;
} else if (newTime > m_lastModifiedTime) {
m_props.clear();
try {
m_props.load(new FileInputStream(getFile()));
} catch (Exception e) {
System.err.println("文件重新读取异常");
e.printStackTrace();
}
}
m_lastModifiedTime = newTime;
String val = m_props.getProperty(name);
if (val == null) {
return defaultVal;
} else {
return val;
}
}
/**
* 读取一特定的属性项
*
* @param name 属性项的项名
* @return 属性项的值(如此项存在), 空(如此项不存在)
*/
public String getConfigItem(String name) {
return getConfigItem(name, "");
}
}
分享到:
相关推荐
[MoonlightService这个string串就是读取配置文件的路径,可以写成宏函数] 3.在判断完m_iniFile是否为空, 接下来就是可以使用了 举例: if (m_iniFile != NULL){ strRet = m_iniFile->GetValue("moonlightService...
保存INI配置文件和读取配置文件,欢迎大家下载学习!
java读取配置文件 在经常更换的一些东西 就可以用配置文件
通过按钮,读取配置文件,并修改自定义配置文件appSettings中的值.这是一个很简单的dome.
利用Java的Properties 类读取配置文件信息! 读取Properties文件的例子
工具类里分读取.yml工具类和.properties工具类,结合博客描述使用,用不到的方法可根据个人情况删除,
Qt读取配置文件的源码
自己写的vbs读取配置文件的代码 直接就能用 很好使哈
利用java可以读取配置文件信息,可以流来处理这个问题
VC读取可以配置文件是比较简单的,但是有时候需要在配置文件增加一些项目,多数时候需要修改代码,我这个简单的例子实现了增加配置文件,但是不需要修改代码的功能,具体应用的时候可以参考思路。
spring读取配置不同目录下的配置文件,使用ClassPathXmlApplicationContext与FileSystemXmlApplicationContext
一个成功的VC读取配置文件的例子,可以直接套用。
读取配置文件获得数字进行计算读取配置文件获得数字进行计算读取配置文件获得数字进行计算
Linux 系统 shell脚本读取配置文件
读取配置文件,封装好的方法,直接调用即可!
WinCE下读取配置文件的类,WinCE5.0下测试通过。
包括了c++读取配置文件的动态链接库及详细使用说明,可以使c++代码对txt文档进行有规律读取
C# .net读取ini配置文件
MFC中读取ini配置文件类工程
使用c++语言使用spdlog日志功能,创建新的message类进行包装,读取外部配置文件进行快速使用,好处可以不用每次都得make程序,只需要修改配置文件就可以直接运行程序