2018-06-12读取和修改配置文件属性

/**

*/
package com.maxtech.card.biz.util;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Properties;

import jxl.common.Logger;

/**

  • @author zhoudengkai
  • @date 2018年3月26日

*/
public class PropertiesUtil {
private static Logger logger = Logger.getLogger(PropertiesUtil.class);

/**
 * 读取配置文件某属性
 * @author zhoudengkai
 * @date 2018年3月26日
 * @param filePath
 * @param key
 */
public static String readValue(String filePath,String key){
    Properties props = new Properties();
    try {
        if(!filePath.startsWith("/")){
            filePath = "/"+filePath;
        }
        InputStream in = PropertiesUtil.class.getResourceAsStream(filePath);
        props.load(in);
        String value = props.getProperty(key);
        return value;
        
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        logger.error(e);
        return null;
    }

}

public static void readProperties(){
    
}


/**
 * 写入配置文件
 * @author zhoudengkai
 * @date 2018年3月26日
 * @param fileName
 * @param parameterName
 * @param parameterValue
 * @throws Exception
 */
public static void writeProperties(String fileName, String parameterName, String parameterValue) throws Exception {
    // 本地测试特别注意,如果是maven项目,请到\target目录下查看文件,而不是源代码下
    // 注意路径不能加 / 了,加了则移除掉
    if (fileName.startsWith("/")){
        fileName.substring(1);
    }
    String filePath = PropertiesUtil.class.getResource("/").getPath()+fileName;
    // 获取配置文件
    Properties pps = new Properties();
    InputStream in = new BufferedInputStream(new FileInputStream(filePath));
    pps.load(in);
    in.close();
    OutputStream out = new FileOutputStream(filePath);
    // 设置配置名称和值
    pps.setProperty(parameterName, parameterValue);
    // comments 等于配置文件的注释
    pps.store(out, "Update " + parameterName + " name");
    out.flush();
    out.close();
}

/*public static void main(String[] args) {
    System.out.println(readValue("/config/url.properties", "pageUrl"));
    String value = PropertiesUtil.readValue("/config/url.properties", "pageUrl_index");
    Jedis js = new Jedis();
    js.set("", "");
    js.get("");
}*/

}

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 本文只针对通过编译方式安装的php添加pdo扩展,没有尝试在yum安装php的情况下实践。 1.安装PDO组件 解...
    右耳朵大鼻子阅读 16,405评论 0 1
  • 翻译国外文章,这是一篇系列文章,2015年的,但是感觉对于国内来说还是比较少见的,所以翻译出来供大家参考。这是一篇...
    少儿创客阅读 3,434评论 0 0
  • 今天是翔哥的街舞考级,考级场地真是可以说是人山人海。走廊全部站满了家长。能看到孩子候场情况的玻璃窗前已经被围得水泄...
    小美宝阅读 3,150评论 0 0
  • 后来哪,也许会慢慢发现,可能你谈过很多次恋爱,可是你真真正正喜欢的人,真正觉得完美无缺的人,就那一个。
    唱旧歌的孤独诗人阅读 1,162评论 0 0
  • 世间就是有些怪癖, 偏要自己没有的东西。 “我们也一样,” 他们说,“我们也一样,我们的灵魂肯定要经历巨大的烦恼!...
    你闻起来像对的人阅读 3,733评论 1 0

友情链接更多精彩内容