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

    转自:

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

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

    [section1]
    key1=value1
     
    [section2]
    key2=value2
     
    。。。。

    原blog中考虑:

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

    代码如下:

      1 import java.io.BufferedReader;
      2 import java.io.FileReader;
      3 import java.io.IOException;
      4 import java.util.ArrayList;
      5 import java.util.HashMap;
      6 import java.util.List;
      7 import java.util.Map;
      8  
      9 /**
     10  * 类名:读取配置类<br>
     11  * @author Phonnie
     12  *
     13  */
     14 public class ConfigReader {
     15      
     16     /**
     17      * 整个ini的引用
     18      */
     19     private Map<String,Map<String, List<String>>>  map = null;
     20     /**
     21      * 当前Section的引用
     22      */
     23     private String currentSection = null;
     24      
     25     /**
     26      * 读取
     27      * @param path
     28      */
     29     public ConfigReader(String path) {
     30         map = new HashMap<String, Map<String,List<String>>>();
     31         try {
     32             BufferedReader reader = new BufferedReader(new FileReader(path));
     33             read(reader);
     34         } catch (IOException e) {
     35             e.printStackTrace();
     36             throw new RuntimeException("IO Exception:" + e);
     37         }
     38          
     39     }
     40  
     41     /**
     42      * 读取文件
     43      * @param reader
     44      * @throws IOException
     45      */
     46     private void read(BufferedReader reader) throws IOException {
     47         String line = null;
     48         while((line=reader.readLine())!=null) {
     49             parseLine(line);
     50         }
     51     }
     52      
     53     /**
     54      * 转换
     55      * @param line
     56      */
     57     private void parseLine(String line) {
     58         line = line.trim();
     59         // 此部分为注释
     60         if(line.matches("^\#.*$")) {
     61             return;
     62         }else if (line.matches("^\[\S+\]$")) {
     63             // section
     64             String section = line.replaceFirst("^\[(\S+)\]$","$1");
     65             addSection(map,section);
     66         }else if (line.matches("^\S+=.*$")) {
     67             // key ,value
     68             int i = line.indexOf("=");
     69             String key = line.substring(0, i).trim();
     70             String value =line.substring(i + 1).trim();
     71             addKeyValue(map,currentSection,key,value);
     72         }
     73     }
     74  
     75  
     76     /**
     77      * 增加新的Key和Value
     78      * @param map
     79      * @param currentSection
     80      * @param key
     81      * @param value
     82      */
     83     private void addKeyValue(Map<String, Map<String, List<String>>> map,
     84             String currentSection,String key, String value) {
     85         if(!map.containsKey(currentSection)) {
     86             return;
     87         }
     88          
     89         Map<String, List<String>> childMap = map.get(currentSection);
     90          
     91         if(!childMap.containsKey(key)) {
     92             List<String> list = new ArrayList<String>();
     93             list.add(value);
     94             childMap.put(key, list);
     95         } else {
     96             childMap.get(key).add(value);
     97         }
     98     }
     99  
    100  
    101     /**
    102      * 增加Section
    103      * @param map
    104      * @param section
    105      */
    106     private void addSection(Map<String, Map<String, List<String>>> map,
    107             String section) {
    108         if (!map.containsKey(section)) {
    109             currentSection = section;
    110             Map<String,List<String>> childMap = new HashMap<String, List<String>>();
    111             map.put(section, childMap);
    112         }
    113     }
    114      
    115     /**
    116      * 获取配置文件指定Section和指定子键的值
    117      * @param section
    118      * @param key
    119      * @return
    120      */
    121     public List<String> get(String section,String key){
    122         if(map.containsKey(section)) {
    123             return  get(section).containsKey(key) ?
    124                     get(section).get(key): null;
    125         }
    126         return null;
    127     }
    128      
    129      
    130      
    131     /**
    132      * 获取配置文件指定Section的子键和值
    133      * @param section
    134      * @return
    135      */
    136     public Map<String, List<String>> get(String section){
    137         return  map.containsKey(section) ? map.get(section) : null;
    138     }
    139      
    140     /**
    141      * 获取这个配置文件的节点和值
    142      * @return
    143      */
    144     public Map<String, Map<String, List<String>>> get(){
    145         return map;
    146     }
    147      
    148 }
    View Code

    实际使用时,认为:

    可以避免一个Key对应多个value的情况。即完全一一对应,则可以简化代码。

    代码如下:

      1 import java.io.BufferedReader;
      2 import java.io.FileReader;
      3 import java.io.IOException;
      4 import java.util.HashMap;
      5 import java.util.List;
      6 import java.util.Map;
      7 import java.util.Scanner;
      8  
      9 /**
     10  * 类名:读取配置类<br>
     11  * @author Phonnie
     12  *
     13  */
     14 public class ConfigReader {
     15      
     16     /**
     17      * 整个ini的引用
     18      */
     19     private HashMap<String,HashMap<String, String> >  map = null;
     20     /**
     21      * 当前Section的引用
     22      */
     23     private String currentSection = null;
     24      
     25     /**
     26      * 读取
     27      * @param path
     28      */
     29     public ConfigReader(String path) {
     30         map = new HashMap<String,HashMap<String, String> >();
     31         try {
     32             BufferedReader reader = new BufferedReader(new FileReader(path));
     33             read(reader);
     34         } catch (IOException e) {
     35             e.printStackTrace();
     36             throw new RuntimeException("IO Exception:" + e);
     37         }
     38          
     39     }
     40  
     41     /**
     42      * 读取文件
     43      * @param reader
     44      * @throws IOException
     45      */
     46     private void read(BufferedReader reader) throws IOException {
     47         String line = null;
     48         while((line = reader.readLine()) != null) {
     49             parseLine(line);
     50         }
     51     }
     52      
     53     /**
     54      * 转换
     55      * @param line
     56      */
     57     private void parseLine(String line) {
     58         line = line.trim();
     59         // 去除空格
     60         //line = line.replaceFirst(" ", "");
     61         //line = line.replaceFirst(" ", "");
     62         
     63         int i = line.indexOf("=");
     64         if (i > 0) {
     65             String left = line.substring(0, i);
     66             String right = line.substring(i + 1);
     67             if (line.charAt(i - 1) == ' '){
     68                 left = line.substring(0, i - 1);
     69             }
     70 
     71             if (line.charAt(i + 1) == ' '){
     72                 right = line.substring(i + 2);
     73             }
     74             line = left + "=" + right;
     75            // System.out.println(line);
     76         }
     77         
     78         // 此部分为注释
     79         if(line.matches("^\#.*$")) {
     80             return;
     81         }else if (line.matches("^\[\S+\]$")) {
     82             // section
     83             String section = line.replaceFirst("^\[(\S+)\]$","$1");
     84             addSection(map,section);
     85         }else if (line.matches("^\S+=.*$")) {
     86             // key ,value
     87             int index = line.indexOf("=");
     88             String key = line.substring(0, index).trim();
     89             String value =line.substring(index + 1).trim();
     90             addKeyValue(map,currentSection,key,value);
     91         }
     92     }
     93  
     94  
     95     /**
     96      * 增加新的Key和Value
     97      * @param map2
     98      * @param currentSection
     99      * @param key
    100      * @param value
    101      */
    102     private void addKeyValue(HashMap<String, HashMap<String, String>> map2,
    103             String currentSection,String key, String value) {
    104         if(!map2.containsKey(currentSection)) {
    105             return;
    106         }
    107          
    108         Map<String, String> childMap = map2.get(currentSection);
    109          
    110         childMap.put(key, value);
    111     }
    112  
    113  
    114     /**
    115      * 增加Section
    116      * @param map2
    117      * @param section
    118      */
    119     private void addSection(HashMap<String, HashMap<String, String>> map2,
    120             String section) {
    121         if (!map2.containsKey(section)) {
    122             currentSection = section;
    123             HashMap<String, String> childMap = new HashMap<String, String>();
    124             map2.put(section, childMap);
    125         }
    126     }
    127      
    128     /**
    129      * 获取配置文件指定Section和指定子键的值
    130      * @param section
    131      * @param key
    132      * @return
    133      */
    134     public String get(String section,String key){
    135         if(map.containsKey(section)) {
    136             if (get(section).containsKey(key))
    137                 return get(section).get(key);
    138             else
    139                 return null;
    140         }
    141         return null;
    142     }
    143      
    144      
    145     /**
    146      * 获取配置文件指定Section的子键和值
    147      * @param section
    148      * @return
    149      */
    150     public HashMap<String, String> get(String section){
    151         if (map.containsKey(section))
    152             return map.get(section);
    153         else
    154             return null;
    155     }
    156      
    157     /**
    158      * 获取这个配置文件的节点和值
    159      * @return
    160      */
    161     public HashMap<String, HashMap<String, String>> get(){
    162         return map;
    163     }
    164 }
    View Code

    使用:

     1 import java.util.HashMap;
     2 
     3 public class ReadConfig {
     4     public static void printMap( HashMap<String,HashMap<String, String> > map ) {
     5         System.out.println("map : ");
     6         for(String section : map.keySet()) {
     7             System.out.println(section);
     8             HashMap<String, String> mp = map.get(section);
     9             for(String key : mp.keySet()) {
    10                 System.out.println(key + " : " + mp.get(key));
    11             }
    12             System.out.println();
    13         }
    14     }
    15 
    16     public static void main(String[] argvs) throws Exception {
    17         System.out.println("Hello World!");
    18         String path = "config2.ini";    
    19         ConfigReader config = new ConfigReader(path);
    20         HashMap<String,HashMap<String, String> > map = config.get();
    21         printMap(map);
    22     }
    23 }
    View Code
  • 相关阅读:
    零基础学习hadoop到上手工作线路指导(初级篇)
    手把手VirtualBox虚拟机下安装rhel6.4 linux 64位系统详细文档
    VMware虚拟机三种网络模式的区别
    python学习笔记(12)--爬虫下载煎蛋网图片
    python学习笔记(11)--爬虫下载漫画图片
    JS学习笔记(5)--一道返回整数数组的面试题(经验之谈)
    python学习笔记(10)--爬虫下载煎蛋图片
    python学习笔记(9)--Python UnicodeEncodeError: 'gbk' codec can't encode character 解决方法
    python学习笔记(8)--爬虫下载占位图片
    python学习笔记(7)--爬虫隐藏代理
  • 原文地址:https://www.cnblogs.com/njczy2010/p/5719242.html
Copyright © 2011-2022 走看看