部分转 Java读取ini配置
转自:
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配置的更多相关文章
- Java读取ini配置
本文转载地址: http://www.cnblogs.com/Jermaine/archive/2010/10/24/1859673.html 不够通用,呵呵. 读取ini的配置的格式如下 ...
- golang 读取 ini配置信息
package main //BY: 29295842@qq.com//这个有一定问题 如果配置信息里有中文就不行//[Server] ;MYSQL配置//Server=localhost ...
- java读取ini文件
ini工具类; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import j ...
- php读取ini配置文件属性
ini的内容格式如下,请根据自己的INI,格式修改下段程序. autostart = false font_size = font_color = red =================== fu ...
- boost::property_tree 读取ini配置
应用场景: 在后端服务器项目开发中,需要初始化一个Socket服务器,需要IP地址与对应端口号等参数:另外还可能因为对接数据库,就还需要数据库的相关配置参数,如我使用的是MySql数据库,就需要数据库 ...
- java 读取ini文件
1.情景:需要将硬代码写到文件中,这样以后改动只需改动灵活 1)txt文件,需要将这code字符串读到代码中,保存成数组 2)导包:pom.xml添加依赖: <dependency> &l ...
- 转 python3 读取 ini配置文件
在代码中经常会通过ini文件来配置一些常修改的配置.下面通过一个实例来看下如何写入.读取ini配置文件. 需要的配置文件是: 1 [path] 2 back_dir = /Users/abc/Pych ...
- 关于自动化测试框架,所需代码技能,Java篇——参数配置与读取.
前言: 说在前边.像我这种假期不出去浪,在这里乖乖写文章研究代码的人,绝壁不是因为爱学习,而是自己不知道去哪玩好,而且也不想玩游戏,看电视剧什么的,结果就无聊到看代码了…… 至于如何解读代码,请把它当 ...
- Java可读取操作系统的配置
/** * Java获取操作系统的配置环境 * @throws Exception */ @Test public void testPro() throws Exception { Properti ...
随机推荐
- 什么是静态代码块?java中如何使用空参构造方法自动生成不同名字的对象,使用非静态的属性和静态属性有什么区别,原因是什么?如何理解static关键字
静态代码块?类加载就执行,最先执行 class demo{ static int num; static{ num=10; num*=3; System.out.println("haha& ...
- 在物理机上,用U盘安装esxi虚拟化环境
一般使用U盘安装centos镜像,可使用镜像刻录工具UltraISO,详细方法参照如下链接: https://jingyan.baidu.com/article/647f0115ee55ba7f214 ...
- Leetcode5078. 负二进制数相加
问题: 5078. 负二进制数相加 给出基数为 -2 的两个数 arr1 和 arr2,返回两数相加的结果. 数字以 数组形式 给出:数组由若干 0 和 1 组成,按最高有效位到最低有效位的顺序排列. ...
- js替换函数用法
定义和用法 replace() 方法用于在字符串中用一些字符替换另一些字符,或替换一个与正则表达式匹配的子串. 语法 stringObject.replace(regexp/substr,replac ...
- GoF23种设计模式之行为型模式之命令模式
一.概述 将一个请求封装为一个对象,从而可以使用不同的请求对客户端进行参数化.对请求排队或记录请求日志,以及支持撤销的操作. 二.适用性 1.当抽象出待执行的动作以参数化某个对象的时候. 2.当需要在 ...
- $(MAKE) , make命令
make 定义了很多默认变量,像常用的命令或者是命令选项之类的,什么CC啊,CFLAGS啊之类.$(MAKE)就是预设的 make 这个命令的名称(或者路径).make -p 可以查看所有预定义的变量 ...
- 重新造轮子之静态链接2(Static linking)
有了上一篇的基础, 这次写一个我们已经滚瓜烂熟的C程序 helloworld .只不过这次我们不能用到到C语言中的main()函数. 实验环境: linux 编译器: gcc 编辑器:Vim 1. ...
- LA 4253 Archery 二分
题意: x轴上方有若干条平行于x轴的线段 在x轴的区间\([0, \, W]\)内找一点发射一条射线,使其穿过所有线段. 问是否存在这样的点. 分析: 我们二分射线端点的坐标,将线段按纵坐标从小到大排 ...
- Python虚拟机函数机制之名字空间(二)
函数执行时的名字空间 在Python虚拟机函数机制之无参调用(一)这一章中,我们对Python中的函数调用机制有个大概的了解,在此基础上,我们再来看一些细节上的问题.在执行MAKE_FUNCTION指 ...
- JSP 页面 jstl 时间戳 long型转时间
转载http://www.cnblogs.com/gmq-sh/p/5528989.html