`
piziyan
  • 浏览: 68428 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论

读取*.cfg文件,保存进HashMap

阅读更多
/**
 * 文本内容样式
 * [SYSTEM]
 * #数据库类型
 * database=oracle
 */
private void init()
{
    BufferedReader reader = null;   
    try
    {
        reader = new BufferedReader(new FileReader(filePath));
        String line;
        while ((line = reader.readLine()) != null)
        {
            line = line.trim();
            //如果行长度为0或者首字节是#或[
            if ((line.length() == 0) || (line.charAt(0) == '#') || (line.charAt(0) == '['))
            {
                continue;
            }
            int splitPos = line.indexOf('=');               
            if (splitPos != -1)
            {
                //等号前为键 等号后为值
                configMap.put(line.substring(0, splitPos).toLowerCase(Locale.ENGLISH).trim(),
                line.substring(splitPos + 1, line.length()).trim());
                    
                }
            }
        }
        catch (Exception e)
        {
            // TODO: handle exception
        }
        finally
        {
            // 关闭文件句柄
            try
            {
                if (reader != null)
                {
                    reader.close();
                }
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
        }            
    }
分享到:
评论
1 楼 gaoxiaoweiandy 2012-01-05  


楼主,那如何 改写 cfg文件里 的 键值呢?

是不是要把文件删除了。重新建立一个文件,然后没行写进去一个键值对。

相关推荐

Global site tag (gtag.js) - Google Analytics