这个例子用使用了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;
}
}