zoukankan      html  css  js  c++  java
  • java后台读取配置文件

    前几天开发时遇到一个问题,在后台读取配置文件的时候无法读取属性值,于是上网查了查,现在在这分享给大家;

    先附上代码吧:

    package com.shafei.util;
    import java.io.BufferedInputStream;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.util.Enumeration;
    import java.util.Properties;
    import org.apache.log4j.Logger;
    public class PropertieUtil {
    private static Logger logger = Logger.getLogger(PropertieUtil.class);
    private PropertieUtil() {
    }
    /**
    * 读取配置文件某属性
    */
    public static String readValue(String filePath, String key) {
    Properties props = new Properties();
    try {
    // 注意路径以 / 开始,没有则处理
    if (!filePath.startsWith("/"))
    filePath = "/" + filePath;
    InputStream in = PropertieUtil.class.getResourceAsStream(filePath);
    props.load(in);
    String value = props.getProperty(key);
    return value;
    } catch (Exception e) {
    logger.error(e);
    return null;
    }
    }
    /**
    * 打印配置文件全部内容(filePath,配置文件名,如果有路径,props/test.properties)
    */
    public static void readProperties(String filePath) {
    Properties props = new Properties();
    try {
    // 注意路径以 / 开始,没有则处理
    if (!filePath.startsWith("/"))
    filePath = "/" + filePath;
    InputStream in = PropertieUtil.class.getResourceAsStream(filePath);
    props.load(in);
    Enumeration<?> en = props.propertyNames();
    // 遍历打印
    while (en.hasMoreElements()) {
    String key = (String) en.nextElement();
    String Property = props.getProperty(key);
    System.out.println(Property);
    logger.info(key + ":" + Property);
    }
    } catch (Exception e) {
    logger.error(e);
    }
    }
    /**
    * 将值写入配置文件
    */
    public static void writeProperties(String fileName, String parameterName, String parameterValue) throws Exception {
    // 本地测试特别注意,如果是maven项目,请到 arget目录下查看文件,而不是源代码下
    // 注意路径不能加 / 了,加了则移除掉
    if (fileName.startsWith("/"))
    fileName.substring(1);
    String filePath = PropertieUtil.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) throws Exception {
    readProperties("jdbc.properties");
    logger.info(readValue("jdbc.properties", "JAVABLOG_WRITE_URL"));
    // writeProperties("conf/test.properties", "dataSource.driver", "test");
    readProperties("conf/test.properties");
    }
    }

    上面的代码我也是学习大牛们的,也不是很难,很实用,希望对大家有帮助!

  • 相关阅读:
    matlab程序性能优化与混合编程技术介绍
    最大熵原理/最大熵原则/最大熵模型(the maximum entropy principle,MEP)
    马氏距离 Mahalanobis Distance
    时间序列分析
    Windows XP + Apache 2.2.4 + PHP 5.2.0 + MySQL 5.0.27 + Zend Optimizer 3.2.0环境配置方法
    栈应用——表达式求值
    Android实现模拟时钟(简单+漂亮)时针、分针、秒针
    基于循环链表的约瑟夫问题
    assert()详解
    Hadoop HPROF 的使用
  • 原文地址:https://www.cnblogs.com/chafe/p/5869874.html
Copyright © 2011-2022 走看看