部分转 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 ...
随机推荐
- UICollectionView实现无限轮播
#import "KGNewsController.h"#import "KGNewsCell.h"#import "KGNews.h"#i ...
- Docker自学纪实(一)Docker介绍
先简单了解一下,做个记录,以便不时之需. Docker简介:Docker 是一个开源的应用容器引擎,基于 Go 语言 并遵从Apache2.0协议开源. Docker 可以让开发者打包他们的应用以及依 ...
- 如何用纯 CSS 创作一个文本淡入淡出的 loader 动画
效果预览 在线演示 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/ERwpeG 可交互视频 ...
- 李涛ps高手之路
下载地址:http://www.ly89.cn/detailR/21.html
- Yii2.0 的安装学习
视频学习地址: 后盾网视频: http://www.houdunren.com/houdunren18_lesson_76?vid=7350 与<Yii框架>不得不说的故事—基础篇 htt ...
- Python基础——字典(dict)
由键-值对构建的集合. 创建 dic1={} type(dic1) dic2=dict() type(dic2) 初始化 dic2={'hello':123,'world':456,'python': ...
- python爬虫基础15-python图像处理,PIL库
Python图像处理-Pillow 简介 Python传统的图像处理库PIL(Python Imaging Library ),可以说基本上是Python处理图像的标准库,功能强大,使用简单. 但是由 ...
- python format 用法详解
format 用法详解 不需要理会数据类型的问题,在%方法中%s只能替代字符串类型 单个参数可以多次输出,参数顺序可以不相同 填充方式十分灵活,对齐方式十分强大 官方推荐用的方式,%方式将会在后面的版 ...
- Linux学习-什么是进程 (process)
触发 任何一个事件时,系统都会将他定义成为一个进程,并且给予这个进程一个 ID ,称为 PID,同时依据启发这个进程的用户与相关属性关系,给予这个 PID 一组有效的权限设定.从此以后,这 个 PID ...
- [转] babel-present-env 与 babel-polyfill 学习总结
babelrc 配置文件 { "presets": [ [ "env", { "modules": false, "useBuil ...