Java如何更新properties的多个值

这个例子用使用了propertie的store方法, 他的优点是简单, 不用自己实现IO,但缺点是回丢失注释和原文件的顺序.如果希望保证顺序,请参考 [转]一个保证store内容顺序不变的properties实现[url]http://ygtq0521.iteye.com/blog/1278081[/url]

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
* Created by IntelliJ IDEA.
* User: Michael
* Date: 10/31/11
* Time: 2:23 PM
* To change this template use File | Settings | File Templates.
*/
public class UpdateProperties {
private String pathSeparator = getPathSeparator();


public static void main(String[] args){

try{
String str1 = "/root/yangxm/sf/DSEngine/work/classpath_3.1.3.tra";
String str2 = "/root/yangxm/sf/DSEngine/work/sandbox-1304059787-0/bin";
String str3 = "/root/yangxm/sf/DSEngine/work/sandbox-1304059787-0/lib";

File traFile = new File(str1);

if(!traFile.exists()){
System.out.println(" tra file not found in dir ...");
}else{
new UpdateProperties().runMain(str1);

}
}catch (IOException e){
System.out.println(e);
// replace logger
} catch (Exception e) {
e.printStackTrace();
}

}

public void runMain(String str1, String str2, String str3) throws Exception{

Properties prop = getTraFile(str1);
String[] targetVars = {
"env1.PATH",
"env2.LD_LIBRARY_PATH",
"env3.SHLIB_PATH",
"env4.LIBPATH"};

Map env = getTraFileValue(prop, targetVars);
Map newEnv = prepareNewEnv(env, str2, str3);
Properties trafile = updateTraFileValue(prop, newEnv);
store(trafile, str1, null);

}

public Properties getTraFile(String properties) throws Exception{
FileInputStream fis = new FileInputStream(new File(properties));
Properties nodeTra = new Properties();
nodeTra.load(fis);
return nodeTra;
}


public String getTraFileValue(Properties prop, String key){
return prop.getProperty(key);
}

public Map<String, String> getTraFileValue(Properties prop, String[] keys){
HashMap<String, String> env = new HashMap<String, String>();
for(String key: keys){
env.put(key, getTraFileValue(prop, key));
}
return env;
}



public boolean isUsingJRE64(Map<String, String> env){
boolean flag = false;
if(env.get("tibco.env.PATH").contains("tibcojre64")) {
flag = true;
}
return flag;
}

public Map<String, String> prepareNewEnv(Map<String, String> env, String binPath, String libPath){
Map<String, String> newEnv = new HashMap<String, String>();
for(String key: env.keySet()){
if(key.equals("tibco.env.PATH")){
newEnv.put(key, appendNewValue(env.get(key), binPath));
}else if(isUsingJRE64(env)){
newEnv.put(key, appendNewValue(env.get(key), libPath + "/64" +
getPathSeparator() + libPath));
}else {
newEnv.put(key, appendNewValue(env.get(key), libPath));
}
}
return newEnv;
}

public String appendNewValue(String initValue, String newValue){
StringBuffer sb = new StringBuffer();
sb.append(newValue).append(pathSeparator).append(initValue);
return sb.toString();
}



public void store(Properties traFile, File file, String comments) throws Exception{
FileOutputStream fos = new FileOutputStream(file);
traFile.store(fos, comments);
}

public void store(Properties traFile, String filePath, String comments) throws Exception{
File file = new File(filePath);
store(traFile, file, comments);
}

public Properties updateTraFileValue(Properties prop, String key, String value){
prop.setProperty(key, value);
return prop;
}

public Properties updateTraFileValue(Properties prop, Map<String, String> map){
for(String key: map.keySet()){
updateTraFileValue(prop, key, map.get(key));
}
return prop;
}


private String getPathSeparator(){
pathSeparator = System.getProperty("path.separator");
return pathSeparator;
}


}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值