转自:

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

部分转 Java读取ini配置的更多相关文章

  1. Java读取ini配置

    本文转载地址:       http://www.cnblogs.com/Jermaine/archive/2010/10/24/1859673.html 不够通用,呵呵. 读取ini的配置的格式如下 ...

  2. golang 读取 ini配置信息

      package main //BY: 29295842@qq.com//这个有一定问题   如果配置信息里有中文就不行//[Server] ;MYSQL配置//Server=localhost   ...

  3. java读取ini文件

    ini工具类; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import j ...

  4. php读取ini配置文件属性

    ini的内容格式如下,请根据自己的INI,格式修改下段程序. autostart = false font_size = font_color = red =================== fu ...

  5. boost::property_tree 读取ini配置

    应用场景: 在后端服务器项目开发中,需要初始化一个Socket服务器,需要IP地址与对应端口号等参数:另外还可能因为对接数据库,就还需要数据库的相关配置参数,如我使用的是MySql数据库,就需要数据库 ...

  6. java 读取ini文件

    1.情景:需要将硬代码写到文件中,这样以后改动只需改动灵活 1)txt文件,需要将这code字符串读到代码中,保存成数组 2)导包:pom.xml添加依赖: <dependency> &l ...

  7. 转 python3 读取 ini配置文件

    在代码中经常会通过ini文件来配置一些常修改的配置.下面通过一个实例来看下如何写入.读取ini配置文件. 需要的配置文件是: 1 [path] 2 back_dir = /Users/abc/Pych ...

  8. 关于自动化测试框架,所需代码技能,Java篇——参数配置与读取.

    前言: 说在前边.像我这种假期不出去浪,在这里乖乖写文章研究代码的人,绝壁不是因为爱学习,而是自己不知道去哪玩好,而且也不想玩游戏,看电视剧什么的,结果就无聊到看代码了…… 至于如何解读代码,请把它当 ...

  9. Java可读取操作系统的配置

    /** * Java获取操作系统的配置环境 * @throws Exception */ @Test public void testPro() throws Exception { Properti ...

随机推荐

  1. bash编程之循环控制:

    bash编程之循环控制: for varName in LIST; do 循环体 done   while CONDITION; do 循环体 done   until CONDITION; do 循 ...

  2. NOIP模拟赛 抓牛

    [题目描述] 农夫约翰被通知,他的一只奶牛逃逸了!所以他决定,马上出发,尽快把那只奶牛抓回来. 他们都站在数轴上.约翰在N(O≤N≤100000)处,奶牛在K(O≤K≤100000)处.约翰有两种办法 ...

  3. 基于Centos7.2搭建Cobbler自动化批量部署操作系统服务

    1       Cobbler服务器端系统环境配置 1.1     系统基本环境准备 [root@cobbler-server ~]# cat /etc/redhat-release CentOS L ...

  4. RSA与AES实现数据加密传输

    RSA.AES简介 RSA:非对称加密,需要提前生成两个密钥(一对的),通过其中一个密钥加密后的数据,只有另一个密钥能解密.通常这两个密钥中有一个会暴漏出来,即对外公开的,这个密钥称为“公钥”,反之另 ...

  5. PHPExcel探索之旅

    学习地址: https://www.imooc.com/video/8359 下载地址: https://packagist.org/packages/phpoffice/phpexcel 用comp ...

  6. vue创建路由,axios前后台交互,element-ui配置使用,django contentType组件

    vue中创建路由 每一个vue组件都有三部分组成 template:放html代码 script:放js相关 style:放css相关 vue中创建路由 1.先创建组件 Course.vue 2.ro ...

  7. 转载:使用Pandas进行数据匹配

    使用Pandas进行数据匹配 本文转载自:蓝鲸的网站分析笔记 原文链接:使用Pandas进行数据匹配 目录 merge()介绍 inner模式匹配 lefg模式匹配 right模式匹配 outer模式 ...

  8. Python头脑风暴3

    驾校是个暴利行业 如果有高学历靠谱的IT人员做驾校教练等等等等.... Python虽然难做企业级应用,但Python是全球个人自定义应用的首选!!!没有之一,所有语言最快的开发速度,最个性化的私人定 ...

  9. 【HIHOCODER 1420】 Bigint Multiplication

    描述 Given 2 nonnegative integers a and b, calculate a × b. 输入 One line with 2 integers a and b separa ...

  10. poj 1017 装箱子问题 贪心算法

    题意:有1*1到6*6的的东西,需要用6*6的箱子将它们装起来.问:至少需要多少个6*6箱子 思路: 一个瓶子怎么装东西最多?先装石头,在装沙子,然后装水. 同样放在本题就是先装6*6然后5*5... ...