zoukankan      html  css  js  c++  java
  • Java读取ini配置

    本文转载地址:

          http://www.cnblogs.com/Jermaine/archive/2010/10/24/1859673.html


    不够通用,呵呵。

    读取ini的配置的格式如下:

    1
    2
    3
    4
    5
    6
    7
    [section1]
    key1=value1
     
    [section2]
    key2=value2
     
    。。。。

    其中可能一个Key对应多个value的情况。

    代码如下:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
     
    /**
     * 类名:读取配置类<br>
     * @author Phonnie
     *
     */
    public class ConfigReader {
         
        /**
         * 整个ini的引用
         */
        private Map<String,Map<String, List<String>>>  map = null;
        /**
         * 当前Section的引用
         */
        private String currentSection = null;
         
        /**
         * 读取
         * @param path
         */
        public ConfigReader(String path) {
            map = new HashMap<String, Map<String,List<String>>>();
            try {
                BufferedReader reader = new BufferedReader(new FileReader(path));
                read(reader);
            } catch (IOException e) {
                e.printStackTrace();
                throw new RuntimeException("IO Exception:" + e);
            }
             
        }
     
        /**
         * 读取文件
         * @param reader
         * @throws IOException
         */
        private void read(BufferedReader reader) throws IOException {
            String line = null;
            while((line=reader.readLine())!=null) {
                parseLine(line);
            }
        }
         
        /**
         * 转换
         * @param line
         */
        private void parseLine(String line) {
            line = line.trim();
            // 此部分为注释
            if(line.matches("^\#.*$")) {
                return;
            }else if (line.matches("^\[\S+\]$")) {
                // section
                String section = line.replaceFirst("^\[(\S+)\]$","$1");
                addSection(map,section);
            }else if (line.matches("^\S+=.*$")) {
                // key ,value
                int i = line.indexOf("=");
                String key = line.substring(0, i).trim();
                String value =line.substring(i + 1).trim();
                addKeyValue(map,currentSection,key,value);
            }
        }
     
     
        /**
         * 增加新的Key和Value
         * @param map
         * @param currentSection
         * @param key
         * @param value
         */
        private void addKeyValue(Map<String, Map<String, List<String>>> map,
                String currentSection,String key, String value) {
            if(!map.containsKey(currentSection)) {
                return;
            }
             
            Map<String, List<String>> childMap = map.get(currentSection);
             
            if(!childMap.containsKey(key)) {
                List<String> list = new ArrayList<String>();
                list.add(value);
                childMap.put(key, list);
            } else {
                childMap.get(key).add(value);
            }
        }
     
     
        /**
         * 增加Section
         * @param map
         * @param section
         */
        private void addSection(Map<String, Map<String, List<String>>> map,
                String section) {
            if (!map.containsKey(section)) {
                currentSection = section;
                Map<String,List<String>> childMap = new HashMap<String, List<String>>();
                map.put(section, childMap);
            }
        }
         
        /**
         * 获取配置文件指定Section和指定子键的值
         * @param section
         * @param key
         * @return
         */
        public List<String> get(String section,String key){
            if(map.containsKey(section)) {
                return  get(section).containsKey(key) ?
                        get(section).get(key): null;
            }
            return null;
        }
         
         
         
        /**
         * 获取配置文件指定Section的子键和值
         * @param section
         * @return
         */
        public Map<String, List<String>> get(String section){
            return  map.containsKey(section) ? map.get(section) : null;
        }
         
        /**
         * 获取这个配置文件的节点和值
         * @return
         */
        public Map<String, Map<String, List<String>>> get(){
            return map;
        }
         
    }
  • 相关阅读:
    在 Linux 下查看硬件配置
    对于 ASP.NET 在 IIS 上的一些高并发处理配置
    处理 目标主机SSH服务存在RC4、CBC或None弱加密算法 的问题
    处理 Windows Server 中 CVE-2016-2183(SSL/TLS) 漏洞的方法
    在 Windows Server 2008 R2下部署 asp.net core 3.1 网站遇到的问题
    在 Windows Server 2012 安装最新版 SSMS 遇到错误 0x80070005 -Acess Denied
    在 CentOS 中安装 7zip
    在 npm run build 时遇到报错 [BABEL] No "exports" main defined in
    【性能项目实战:k8s+微服务】热门测试技术,提升职场竞争力(持续更新中。。。)
    windows下安装skywalking8.6.0(用于本地开发调试代码)
  • 原文地址:https://www.cnblogs.com/hthuang/p/4361865.html
Copyright © 2011-2022 走看看