向properties文件中写入信息(针对获取properties文件失败的总结)

 前段时间项目需要将某个属性动态的写入项目发布路径下的properties文件中;但是实际发布时发现找不到maven项目resource路径下的project.properties文件,调试多次代码如下:

/**
     * 写入properties信息
     * 
     * @param key 名称
     * @param value 值
     */
    public static void modifyConfig(String key, String value) {
        try {
            // 从输入流中读取属性列表(键和元素对)
            Properties prop = getProperties();
            prop.setProperty(key, value);
            //Global.class为当前类名,project.properties为路径
            String path = Global.class.getResource("/project.properties").getPath();
            FileOutputStream outputFile = new FileOutputStream(path);
            prop.store(outputFile, "modify");
            outputFile.close();
            outputFile.flush();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

 

posted @ 2016-09-01 14:05  代码书生  阅读(2218)  评论(1编辑  收藏  举报