代码修改配置文件的参数值

/**
* 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("\n");
} else {
buffer.append(strKey).append("=").append(strValue).append("\n");
;
}

}

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;
}
}
}

 


}

转载于:https://www.cnblogs.com/Syney/p/7886372.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值