Chinaunix首页 | 论坛 | 博客
  • 博客访问: 26187867
  • 博文数量: 2065
  • 博客积分: 10377
  • 博客等级: 上将
  • 技术积分: 21525
  • 用 户 组: 普通用户
  • 注册时间: 2008-11-04 17:50
文章分类

全部博文(2065)

文章存档

2012年(2)

2011年(19)

2010年(1160)

2009年(969)

2008年(153)

分类: Java

2010-07-13 22:21:41

package cn.util;

import java.io.FileInputStream;
import java.io.IOException;

public interface TyINIUtils {
    /**
     * 从INI配置文件中读取变量的值
     * @param file配置文件的路径
     * @param section 要获取的变量所在的段
     * @param variable 要获取的变量名称
     * @param defaultValue 变量名称不存在时的默认值
     * @return 变量的值
     * @throws IOException抛出文件操作可能的异常
     * */

    public String getProfileString(String file,String section,String variable,String defaultValue) throws IOException;
    
    /**
     * 修改INI配置文件中变量的值
     * @param file 配置文件的路径
     * @param section 要修改的变量所在的段
     * @param variable 要修改的变量的名称
     * @param value 变量的新值
     * @throws IOException 抛出文件操作的可能异常
     * */

    public boolean setProfileString(String file,String section,String variable,String value) throws IOException;
}


实现的类代码:

/**
 * INI格式为:
 * [settings]
    url=127
    dbname=abc
    [setting]
    url=12
 * */

package cn.util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class TyINIUtil implements TyINIUtils {

    public String getProfileString(String file, String section,
            String variable, String defaultValue) throws IOException {
        String strLine,valueString="";
        BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
        boolean isInSection = false;
        try {
            while((strLine = bufferedReader.readLine()) != null) {
                //读取INI文件到内存

                strLine = strLine.trim();
                Pattern p;
                Matcher m,m2;
                p = Pattern.compile("\\[\\s*" + section + "\\s*\\]");
                Pattern p2 = Pattern.compile("\\[.*\\]");
                m = p.matcher((strLine));
                m2 = p2.matcher((strLine));
                if (m2.find()) {
                    if (m.find()) {
                        isInSection = true;
                    } else {
                        isInSection = false;
                    }
                }
                if (isInSection == true) {
                    strLine = strLine.trim();
                    String[] strArray = strLine.split("=");
                    if (strArray.length == 2) {
                        valueString = strArray[0].trim();
                        if (valueString.equalsIgnoreCase(variable)) {
                            valueString = strLine.substring(strLine.indexOf("=") + 1).trim();
                            return valueString;
                        }
                    }
                }
                
            }
        } catch (Exception e) {
            // TODO: handle exception

        } finally {
            bufferedReader.close();
        }
        return defaultValue;
    }

    public boolean setProfileString(String file, String section,
            String variable, String value) throws IOException {
        String fileContent,allLine;
        BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
        boolean isInSection = false;
        fileContent = "";
        try {
            while((allLine = bufferedReader.readLine())!= null) {
                allLine = allLine.trim();
                Pattern p;
                Matcher m,m2;
                p = Pattern.compile("\\[\\s*" + section + "\\s*\\]");
                Pattern p2 = Pattern.compile("\\[.*\\]");
                m = p.matcher((allLine));
                m2 = p2.matcher((allLine));
                if (m2.find()) {
                    if (m.find()) {
                        isInSection = true;
                    } else {
                        isInSection = false;
                    }
                }
                if (isInSection == true) {
                    String[] strArray = allLine.split("=");
                    if (strArray.length == 2) {
                        String valueString = strArray[0].trim();
                        if (valueString.equalsIgnoreCase(variable)) {
                            String newLine = valueString + "=" + value;
                            fileContent += newLine + "\r\n";
                        } else {
                            fileContent += allLine + "\r\n";
                        }
                    } else {
                        fileContent += allLine + "\r\n";
                    }
                } else {
                    fileContent += allLine + "\r\n";
                }
            }
            System.out.println(fileContent);
            BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(file,false));
            bufferedWriter.write(fileContent);
            bufferedWriter.flush();
            bufferedWriter.close();
            return true;
        } catch (Exception e) {
            // TODO: handle exception

            e.printStackTrace();
        } finally {
            bufferedReader.close();
        }
        return false;
    }
    public static void main(String[] args) {
        try {
            TyINIUtil obj = new TyINIUtil();
            System.out.println(obj.getProfileString("d:/1.ini", "setting", "url", "0"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
}


阅读(1038) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2010-07-18 08:51:43

近期努力:UNIX编程、存储、PYTHON高级编程、NOSQL数据库、BASH。