开发者社区> 问答> 正文

关于properties文件的读写:报错

刚入坑小白一枚
properties文件读写方法网上找的,写test的时候,也可以成功,但是当写入到项目里面的时候,会报路径错误,这个是什么原因?之后我把方法封装成工具,可以顺利的读取,写入之后也可以读取,但是,写入的时候,我查看properties文件的值却没有改变。麻烦大神指点一下!
PS:properties是用来存放一个动态密钥,15分钟的有效期,最开始考虑的是存入在DB,但是考虑数据库访问,所以改为放入在资源文件。不知道这样考虑正确不正确。
同时,有个方法里面有两个接口,两个接口都能达到业务需求,但是,要求数据按照一定的比例来调取其中的一个,比如说3:7,3层数据使用第一个接口,7层数据使用第二个接口。所以在properties里面存放一个值来计数。。。大神指点一下这个比例应该怎么做?

展开
收起
kun坤 2020-06-09 11:46:00 745 0
1 条回答
写回答
取消 提交回答
  • 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。。。能讲更详细一些吗?

    2020-06-09 11:46:09
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载