import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URL;
import java.util.Properties;
/**
* Properties配置文件修改后重读的功能
*
* @author Administrator
*
*/
public class ConfigUtil {
private static Properties props = null;
private static File configFile = null;
private static long fileLastModified = 0L;
private static void init() {
URL url = ConfigUtil.class.getClassLoader().getResource(
"global.properties");//global.properties放到WEB-INF/classes/
configFile = new File(url.getFile());
fileLastModified = configFile.lastModified();
props = new Properties();
load();
}
private static void load() {
try {
props.load(new FileInputStream(configFile));
fileLastModified = configFile.lastModified();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public static String getConfig(String key) {
if ((configFile == null) || (props == null))
init();
if (configFile.lastModified() > fileLastModified)
load();
return props.getProperty(key);
}
public static void main(String[] arg){
String userName = ConfigUtil.getConfig("userName");
System.out.println(userName);
}
}
分享到:
相关推荐
log4j.properties配置文件
主要介绍了Python实现读取Properties配置文件的方法,结合实例形式分析了Python读取Properties配置文件类的定义与使用相关操作技巧,需要的朋友可以参考下
java如何读取properties配置文件
java读写properties配置文件
properties文件获取工具类:静态加载properties配置文件,有根据key获取值的方法
利用java的反射解析Properties文件转成对象 /** * 解析properties文件为对象 * @param * @param propPath * @param cls * @return * @throws InstantiationException * @throws ...
activiti和springboot整合只使用application.properties配置文件,解决了jdbc长时间待机连接被收回报错。使用springProcessEngineConfiguration对activiti管理
c#操作properties,读写配置文件,非常方便,几行代码就搞定
springboot_properties配置项
配置文件上传.properties配置文件上传.properties配置文件上传.properties配置文件上传.properties配置文件上传.properties
IDEA WEB项目启动不加载application.properties配置文件,yml配置文件是否也会不加载未尝试,如果有遇到的可以尝试一下。
Liferay 6.0.5 portal.properties 配置文件详解 详细说明了 portal.properties 配置文件可以配置的所有属性
Log4j.jar包,和log4j.properties属性文件打包下载.
大家都喜欢把配置文件放在src目录下,如果有10个以上的配置文件为什么不考虑在WEB-INF目录下新建一个文件夹,专门放配置文件;这样即好管理,文件安全性又高。亲问题已经解决,把源代码共享给大家,已经通过测试;...
写好的读取properties配置文件的PropertiesConfig.java类,只需new个对象,PropertiesConfig config = new PropertiesConfig(); config.setPropertiesDataSource("/jdbc.properties"); config.getString(...
主要为大家详细介绍了SpringBoot获取yml和properties配置文件的内容,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
使用Java中的Properties类的方法,以及类加载器来读取配置文件中的信息。可以读取.properties文件和.cfg文件。详细代码+注释+测试例子
log4j中使用的jar包,版本为1.2.17,下载即可使用,满足idea和eclipse使用,并附有配置文件
java对properties配置文件的读和写
SSM框架下的几种加载properties配置文件方式,博客内含介绍https://blog.csdn.net/weixin_42803662