package com.qiao.java;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Properties;
import org.jboss.util.Primitives;
import com.yamu.basic.StringUtils;
public class IniReader {
protected HashMap sections = new LinkedHashMap();
private transient String currentSecion;
private transient Properties current;
private final String filePath=ConfigUtil.getInstance().getValue("config_ini_filepath");
public static IniReader iniFile=new IniReader();
public static IniReader getInstance(){
return iniFile;
}
private IniReader() {}
private void read() throws IOException {
// modifyed by brmrk
BufferedReader reader = new BufferedReader(new InputStreamReader(
new FileInputStream(filePath), "UTF-8"));
String line;
while ((line = reader.readLine()) != null) {
parseLine(line);
}
reader.close();
}
/**
* 描述:处理读取ini文件的每一行
* @author: erguai123@yahoo.com.cn
* @createtime: 2010-5-11 下午02:53:36
*/
protected void parseLine(String line) {
line = line.trim();
if (line.matches("//[.*//]")) {
currentSecion = line.replaceFirst("//[(.*)//]", "$1");
current = new Properties();
} else if (line.matches(".*=.*")) {
int i = line.indexOf('=');
String name = line.substring(0, i);
String value = line.substring(i + 1);
current.setProperty(name.trim(), value.trim());
sections.put(currentSecion, current);
}
}
/**
* 描述:根据传入的片段名称和值的名字获取对应的数值
* @author: erguai123@yahoo.com.cn
* @createtime: 2010-5-11 下午02:54:09
*/
public String getValue(String section, String name) throws IOException {
System.out.println("section="+section+"name="+name);
this.read();
Properties p = (Properties) sections.get(section);
if (p == null) {
return null;
}
String value = p.getProperty(name);
System.out.println("-------------------------get value="+value);
return value;
}
/**
*
* 描述:根据片段名字,值的名字,值的数值进行更新
* @author: erguai123@yahoo.com.cn
* @createtime: 2010-5-11 下午02:54:40
*/
public boolean changeProperty(String sectionName,String key,String value) throws IOException,IllegalArgumentException{
if(!StringUtils.isEmpty(key)){
return false;
}else{
this.read();//重新读取文件到内存中
Properties p=(Properties)sections.get(sectionName);
if(null == p){
throw new IllegalArgumentException("sectionName参数出错");
}
p.setProperty(key, value);
this.writeProperty();
return true;
}
}
/**
*
* 描述:将修改后的值写入配置文件
* @author: erguai123@yahoo.com.cn
* @createtime: 2010-5-11 下午02:55:22
*/
private void writeProperty() throws IOException{
PrintWriter writer=new PrintWriter(new OutputStreamWriter(new FileOutputStream(filePath)));
try {
StringBuilder builder=new StringBuilder();
Iterator<String> iterator=sections.keySet().iterator();
while(iterator.hasNext()){
String sectionName=iterator.next();
Properties p=(Properties)sections.get(sectionName);
builder.append("/n").append("/n").append("[").append(sectionName).append("]");
builder.append(getPropertiesString(p));
}
writer.write(builder.toString().trim());
writer.flush();
writer.close();
} finally{
if(writer != null){
writer.close();
}
}
}
/**
*
* 描述:获取组织好的properties文件的字符串表示
* @author: erguai123@yahoo.com.cn
* @createtime: 2010-5-11 下午02:55:49
*/
private String getPropertiesString(Properties p){
StringBuilder builder=new StringBuilder();
Iterator iterator=p.keySet().iterator();
while(iterator.hasNext()){
String key=iterator.next().toString();
builder.append("/n").append(key).append("=").append(p.get(key));
}
return builder.toString();
}
}