2.Properties操作工具类

public class ConfigKit
{

private static Logger logger = Logger.getLogger(ConfigKit.class);

/**
* 增加属性文件值
* @param key
* @param value
*/
public static void addProperties(String key[], String value[], String file)
{
Properties iniFile = getProperties(file);
FileOutputStream oFile = null;
try
{
iniFile.put(key, value);
oFile = new FileOutputStream(file, true);
iniFile.store(oFile, "modify properties file");
}
catch (FileNotFoundException e)
{
logger.warn("do " + file + " FileNotFoundException:", e);
}
catch (IOException e)
{
logger.warn("do " + file + " IOException:", e);
}
finally
{
try
{
if (oFile != null)
{
oFile.close();
}
}
catch (IOException e)
{
logger.warn("do " + file + " IOException:", e);
}
}
}

/**
* 读取配置文件
* @return
*/
public static Properties getProperties(String file)
{
Properties pro = null;
// 从文件mdxbu.properties中读取网元ID和模块ID信息
FileInputStream in = null;
try
{
in = new FileInputStream(file);
pro = new Properties();
pro.load(in);

}
catch (Exception e)
{
logger.warn("Read " + file + " IOException:", e);
}
finally
{
try
{
if (in != null)
{
in.close();
}
}
catch (IOException e)
{
logger.warn("Read " + file + " IOException:", e);
}
}
return pro;
}

/**
* 保存属性到文件中
* @param pro
* @param file
*/
public static void saveProperties(Properties pro, String file)
{
if (pro == null)
{
return;
}
FileOutputStream oFile = null;
try
{
oFile = new FileOutputStream(file, false);
pro.store(oFile, "modify properties file");
}
catch (FileNotFoundException e)
{
logger.warn("do " + file + " FileNotFoundException:", e);
}
catch (IOException e)
{
logger.warn("do " + file + " IOException:", e);
}
finally
{
try
{
if (oFile != null)
{
oFile.close();
}
}
catch (IOException e)
{
logger.warn("do " + file + " IOException:", e);
}
}
}

/**
* 修改属性文件
* @param key
* @param value
*/
public static void updateProperties(String key, String value, String file)
{
//key为空则返回
if (key == null || "".equalsIgnoreCase(key))
{
return;
}
Properties pro = getProperties(file);
if (pro == null)
{
pro = new Properties();
}
pro.put(key, value);

//保存属性到文件中
saveProperties(pro, file);
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值