JAVA读取ini文件的一个类,存着以备后用!

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值