转自:

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

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

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

原blog中考虑:

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

代码如下:

 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;
} }

实际使用时,认为:

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

代码如下:

 import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner; /**
* 类名:读取配置类<br>
* @author Phonnie
*
*/
public class ConfigReader { /**
* 整个ini的引用
*/
private HashMap<String,HashMap<String, String> > map = null;
/**
* 当前Section的引用
*/
private String currentSection = null; /**
* 读取
* @param path
*/
public ConfigReader(String path) {
map = new HashMap<String,HashMap<String, 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();
// 去除空格
//line = line.replaceFirst(" ", "");
//line = line.replaceFirst(" ", ""); int i = line.indexOf("=");
if (i > 0) {
String left = line.substring(0, i);
String right = line.substring(i + 1);
if (line.charAt(i - 1) == ' '){
left = line.substring(0, i - 1);
} if (line.charAt(i + 1) == ' '){
right = line.substring(i + 2);
}
line = left + "=" + right;
// System.out.println(line);
} // 此部分为注释
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 index = line.indexOf("=");
String key = line.substring(0, index).trim();
String value =line.substring(index + 1).trim();
addKeyValue(map,currentSection,key,value);
}
} /**
* 增加新的Key和Value
* @param map2
* @param currentSection
* @param key
* @param value
*/
private void addKeyValue(HashMap<String, HashMap<String, String>> map2,
String currentSection,String key, String value) {
if(!map2.containsKey(currentSection)) {
return;
} Map<String, String> childMap = map2.get(currentSection); childMap.put(key, value);
} /**
* 增加Section
* @param map2
* @param section
*/
private void addSection(HashMap<String, HashMap<String, String>> map2,
String section) {
if (!map2.containsKey(section)) {
currentSection = section;
HashMap<String, String> childMap = new HashMap<String, String>();
map2.put(section, childMap);
}
} /**
* 获取配置文件指定Section和指定子键的值
* @param section
* @param key
* @return
*/
public String get(String section,String key){
if(map.containsKey(section)) {
if (get(section).containsKey(key))
return get(section).get(key);
else
return null;
}
return null;
} /**
* 获取配置文件指定Section的子键和值
* @param section
* @return
*/
public HashMap<String, String> get(String section){
if (map.containsKey(section))
return map.get(section);
else
return null;
} /**
* 获取这个配置文件的节点和值
* @return
*/
public HashMap<String, HashMap<String, String>> get(){
return map;
}
}

使用:

 import java.util.HashMap;

 public class ReadConfig {
public static void printMap( HashMap<String,HashMap<String, String> > map ) {
System.out.println("map : ");
for(String section : map.keySet()) {
System.out.println(section);
HashMap<String, String> mp = map.get(section);
for(String key : mp.keySet()) {
System.out.println(key + " : " + mp.get(key));
}
System.out.println();
}
} public static void main(String[] argvs) throws Exception {
System.out.println("Hello World!");
String path = "config2.ini";
ConfigReader config = new ConfigReader(path);
HashMap<String,HashMap<String, String> > map = config.get();
printMap(map);
}
}

最新文章

  1. Arcgis Server 10.2默认服务端口号修改方法
  2. 解决 -ERR Plaintext authentication disallowed on non-secure (SSL/TLS) connections 方案[sendmail, dovecot]
  3. Asp.net 与 jsp 交互 (打开或跳转页面)
  4. 二模 (11) day2
  5. vector内存分配
  6. Charles是Mac的Fiddler抓包工具
  7. php实现查询百度google收录情况(示例代码)
  8. Oracle的表连接方式
  9. 玩转html5(一)-----盘点html5新增的那些酷酷的input类型和属性
  10. 国内ng学习网站
  11. java I/O---复制文本文件
  12. QT之Http请求
  13. tornado--输入和输出
  14. tiny4412--linux驱动学习(1)
  15. Linux分区的几种方案
  16. 『Python CoolBook』C扩展库_其六_从C语言中调用Python代码
  17. ant通配符
  18. 九,ESP8266 判断是断电上电(强制硬件复位)之后运行的内部程序还是内部软件复位之后运行的程序(基于Lua脚本语言)
  19. word中怎样设置页码包含总页数
  20. POJ 2376 Cleaning Shifts (贪心,区间覆盖)

热门文章

  1. 01_12_JSP简介
  2. Java多线程 编写三各类Ticket、SaleWindow、TicketSaleCenter分别代表票信息、售票窗口、售票中心。 售票中心分配一定数量的票,由若干个售票窗口进行出售,利用你所学的线程知识来模拟此售票过程。
  3. TabControl重写,添加关闭按钮
  4. git 指令记录
  5. sphinx 快速使用
  6. Redis的安装、服务配置
  7. 03 Django视图
  8. 用python编写简易登录接口
  9. 利用for循环和range输出9 * 9乘法口诀表
  10. VS重置开发环境的方法