ini文件中保存的信息格式为:
[module]
    var = [val]我用如下方式读取,不能正确的取出来:
public static synchronized 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(strLine.indexOf("=") > 0)
                    {
                        System.out.println("inifile test:******************"+strLine);                        valueString = strArray[0].trim();
                        System.out.println("inifile test*********************:"+valueString);
                        if (valueString.equalsIgnoreCase(variable))
                        {
                            valueString = strLine.substring(strLine.indexOf("=") + 1).trim();
                            return valueString;
                        }                    }
                 
                }            }
        } catch (Exception e)
        {
            // TODO: handle exception        } finally
        {
            bufferedReader.close();
        }
        return defaultValue;
    }主要是因为【】的原因,我去掉【】就能取出来了,这是为什么?

解决方案 »

  1.   

    Csdn动不动回复就不好用
      

  2.   

    [module]
        var = [val] p = Pattern.compile("\\[\\s*" + section + "\\s*\\]");
    Pattern p2 = Pattern.compile("\\[.*\\]");你上面的这个正则表达式获取的是标红色的部分.而你下面的这部分代码又是希望获取带等号的整行.所以肯定是不行的.if (isInSection == true) {
    strLine = strLine.trim();
    String[] strArray = strLine.split("=");
    if (strLine.indexOf("=") > 0) {
    System.out.println("inifile test:******************" + strLine); valueString = strArray[0].trim();
    System.out.println("inifile test*********************:" + valueString);
    if (valueString.equalsIgnoreCase(variable)) {
    valueString = strLine.substring(strLine.indexOf("=") + 1).trim();
    return valueString;
    } } }
      

  3.   


    while循环按行取,然后通过各个条件判断。我去掉【】就能取出=后的string