部分转 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 ...
随机推荐
- Linux性能检测常用的10个基本命令
检测性能的10个命令汇总 uptim dmesg | tail vmstat 1 mpstat -P ALL 1 pidstat 1 iostat -xz 1 free -m sar -n DEV 1 ...
- OI杂记
从今天开始记录一下为数不多天的OI历程 8.25 上 今天举行了难得的五校联考,模拟noip,题目的解压密码竟然是$aKnoIp2o18$,对你没有看错!!! 7:50老师?啊啊啊啊,收不到题目啊,还 ...
- JS - 生成UUID
function uuid(len, radix) { var chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvw ...
- python爬虫用到的一些东西
原装requests >>> import requests >>> response = requests.get('http://www.baidu.com') ...
- Linux菜鸟起飞之路【五】权限管理(一)
一.与用户相关的几个文件 1./etc/passwd 储存用户名,格式为 用户名:密码(用密码代位符X代替):UID:GID:用户描述信息:家目录:shell 用户名(login_name):是代表用 ...
- 使用powershell批量更新git仓库
Get-ChildItem D:\GitHub\NetCore | ForEach-Object -Process{ cd $_.name; git pull; cd ../ }
- python入门:BREAK 的用法 跳当前循环后,不再执行下面代码块
#!/urs/bin/env python # -*- coding:utf-8 -*- # BREAK 的作用 跳当前循环后,不再执行下面代码块 while True: ') break ') #w ...
- 数字内置方法详解(int/long/float/complex)
一.常用方法 1.1.int 以下是Python2.7的int内置函数: 序号 函数名 作用 举例 1 int.bit_length() 二进制存储这个整数至少需要多少bit(位). >> ...
- 【php】php安全问题
使用 —enable-force-cgi-redirect 选项 设置 doc_root 或 user_dir 或 open_basedir PHP运行的用户身份不能为ROOT 数据库字段加密 程序不 ...
- python之函数基础总结
定义:函数是指将一组语句的集合通过一个名字(函数名)封装起来,要想执行这个函数,只需调用其函数名即可. def sayhi(name): print("Hello, %s, I', nobo ...