zoukankan      html  css  js  c++  java
  • 代码修改配置文件的参数值

    /**
    * IPMTPP Config.java
    * 2013-5-29
    */
    package cn.ipanel.msg.util.common;

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.net.URL;
    import java.util.Enumeration;
    import java.util.Properties;

    import com.ipanel.webapp.framework.util.Log;

    public class Log4jConfig {

    final static String CONFIG_FILE = "log4j.properties";
    private static final String TAG = "Log4jConfig";
    static Properties props = new Properties();
    static URL url=null;
    static {

    url = Log4jConfig.class.getClassLoader().getResource(CONFIG_FILE);


    }

    public synchronized static String getConfig(String name, String defaultValue) {
    try {
    props.load(new FileInputStream(new File(url.getFile())));
    } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    return props.getProperty(name, defaultValue);
    }

    public synchronized static String getConfig(String name) {
    return getConfig(name, null);
    }

    public static String getRootLogger() {
    return getConfig("log4j.rootLogger");
    }

    public static void setRootLogger(String type) throws Exception {
    StringBuffer buffer = new StringBuffer();

    props.load(new FileInputStream(new File(url.getFile())));
    Enumeration en = props.propertyNames(); // 得到配置文件的名字
    while (en.hasMoreElements()) {
    String strKey = (String) en.nextElement();
    String strValue = props.getProperty(strKey);
    Log.i(TAG, strKey + "=" + strValue);
    if ("log4j.rootLogger".equals(strKey)) {

    buffer.append("log4j.rootLogger").append("=").append((type))
    .append(" ");
    } else {
    buffer.append(strKey).append("=").append(strValue).append(" ");
    ;
    }

    }

    FileOutputStream fout = null;
    try {

    fout = new FileOutputStream(new File(url.getFile()));
    fout.write(buffer.toString().getBytes());
    } catch (Exception e) {
    throw e;
    } finally {
    if (fout != null)
    try {
    fout.close();
    fout = null;
    } catch (IOException e) {
    throw e;
    }
    }
    }


    }

  • 相关阅读:
    凸包学习笔记
    2019ICPC南昌网络赛总结
    结对编程作业
    实验 6 :OpenDaylight 实验 ——OpenDaylight 及 Postman 实现流表下发
    实验 5:OpenFlow 协议分析和 OpenDaylight 安装
    实验 3:Mininet 实验——测量路径的损耗率
    实验 4 : Open vSwitch 实验——Mininet 中使用 OVS 命令
    第一次个人编程作业
    实验 2 :Mininet 实验 —— 拓扑的命令脚本生成
    实验 1 :Mininet 源码安装和可视化拓扑
  • 原文地址:https://www.cnblogs.com/Syney/p/7886372.html
Copyright © 2011-2022 走看看