刚入坑小白一枚
properties文件读写方法网上找的,写test的时候,也可以成功,但是当写入到项目里面的时候,会报路径错误,这个是什么原因?之后我把方法封装成工具,可以顺利的读取,写入之后也可以读取,但是,写入的时候,我查看properties文件的值却没有改变。麻烦大神指点一下!
PS:properties是用来存放一个动态密钥,15分钟的有效期,最开始考虑的是存入在DB,但是考虑数据库访问,所以改为放入在资源文件。不知道这样考虑正确不正确。
同时,有个方法里面有两个接口,两个接口都能达到业务需求,但是,要求数据按照一定的比例来调取其中的一个,比如说3:7,3层数据使用第一个接口,7层数据使用第二个接口。所以在properties里面存放一个值来计数。。。大神指点一下这个比例应该怎么做?
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.*;
import java.util.Properties;
/**
* 属性工具
*/
public class PropertiesUtil {
private static final Logger LOGGER = LoggerFactory.getLogger(PropertiesUtil.class);
/**
* 属性文件名称.
*/
private final static String PROPERTIES_FILENAME = "application.properties";
/**
* 配置属性对象静态化
*/
private static Properties properties;
public static Properties getProperties() {
if (properties == null) {
synchronized (PropertiesUtil.class) {
if (properties == null) {
loadProperties(PROPERTIES_FILENAME);
}
}
}
return properties;
}
/**
* 根据key获取配置的字符串value值
*
* @param key
* @return
*/
public static String getProperty(String key) {
if (key == null) {
return null;
}
return getProperties().getProperty(key);
}
public static void setProperty(String key,String value) {
if (StringUtils.isNotBlank(key) && StringUtils.isNotBlank(value)) {
getProperties().setProperty(key,value);
}
}
/**
* 更新属性文件
*/
public static void updateProperties(){
OutputStream fos = null;
try {
fos = new FileOutputStream(new File(Thread.currentThread().getContextClassLoader().getResource(PROPERTIES_FILENAME).getFile()));
getProperties().store(fos, "update value");
} catch (IOException e) {
LOGGER.error("udpate value error :{}", e);
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
LOGGER.error("close error :{}", e);
}
}
}
}
/**
* 根据key获取配置的数字value值
*
* @param key
* @return
*/
public static int getNumerProperty(String key) {
String value = getProperties().getProperty(key);
if (NumberUtils.isNumber(value)) {
return Integer.parseInt(value);
} else {
return 0;
}
}
public static void loadProperties(Properties props) {
properties = props;
}
/**
* 根据指定的文件名称,从类路径中加载属性文件,构造Properties对象
*
* @param filename 属性文件名称
*/
public static void loadProperties(String filename) {
InputStream in = null;
ClassLoader threadContextClassLoader = Thread.currentThread().getContextClassLoader();
properties = new Properties();
if (threadContextClassLoader != null) {
in = threadContextClassLoader.getResourceAsStream(filename);
}
if (in == null) {
in = PropertiesUtil.class.getResourceAsStream(filename);
if (in == null) {
LOGGER.warn("No properties file found in the classpath by filename " + filename);
}
} else {
try {
properties.load(in);
LOGGER.info("Properties read " + properties);
} catch (Exception e) {
LOGGER.error("Error reading from " + filename, e);
} finally {
try {
in.close();
} catch (IOException e) {
LOGGER.error("IOException while closing InputStream: " + e.getMessage());
}
}
}
}
} 上面是 获取properties信息,更新的代码
######回复 @星无宇 : 你给的工具一样不能更改了properties里的value。先使用set方法,再get读取,输出,可以得到新值,但是properties里面的值不改变,不知道为什么。######回复 @花儿向太阳 : 15分钟的有效期 ,可以在第一次生产密钥的时候, 往properties里面 增加一个 时间戳, 每次获取密钥的时候,校验下properties的时间戳和当前时间的间隔, 大于15分钟就 更新密钥 .######先谢谢,我先试试你给的工具。。######动态密钥,15分钟的有效期 -- 只看这点,我觉得你应该放在cache中######回复 @zakari : 谢谢,我去看看相关的资料######回复 @花儿向太阳 : 你可以找一下 encache 的相关资料, 可以配置缓存的失效时间######回复 @花儿向太阳 : 网上随便找的,这类资料比较多,参考下http://developer.51cto.com/art/201411/456219.htm######因为小白,所以不会cache。。。能讲更详细一些吗?