Java读取ini配置
本文转载地址:
http://www.cnblogs.com/Jermaine/archive/2010/10/24/1859673.html
不够通用,呵呵。
读取ini的配置的格式如下:
1
2
3
4
5
6
7
|
[section1] key1=value1 [section2] key2=value2 。。。。 |
其中可能一个Key对应多个value的情况。
代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
|
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; } } |
Java读取ini配置的更多相关文章
- 部分转 Java读取ini配置
转自: http://www.cnblogs.com/Jermaine/archive/2010/10/24/1859673.html 读取ini的配置的格式如下: [section1] key1=v ...
- 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 ...
随机推荐
- android开发用无线网络进行Android开发中的调试
1.手机具有root权限 2.安装adbWireless1.5.4.apk (下面有下载地址) 3.敲入命令:adb connect 192.168.1.127 后面是手机的IP地址 打开eclip ...
- 2.Java集合总结系列:List接口及其实现
在介绍List接口之前,我们先来看看 Collection 接口,因为Collection接口是 List / Set / Queue 接口的父接口,List / Set / Queue 的实现类中很 ...
- 漫谈PHP代码规范
前言 虽说PHP是世界上最好的语言,但是写出来的PHP代码却往往不是最美观的.究其原因,可能正式因为PHP简单易上手,适合快速迭代的特性,导致了我们沉浸在迅速完成需求迭代的窃喜中,却忘记了规范性.忽略 ...
- MySQL学习笔记(二)—查询
一.多表连接查询 新建两张表t_user.t_order. 1.内连接 返回满足条件的所有记录. (1)显式内连接 使用inner join关键字,在on ...
- NodeJs之http
创建新的服务器 创建一个简单的服务 var http = require("http"); var server = http.createServer(); server.lis ...
- 实现简单的跨站脚本攻击(XSS)
我们来通俗的了解一下什么是跨站脚本攻击(XSS):在表单中提交 一段 js代码 ,提交的内容被展示到页面时 ,js会被浏览器解析 打个比方吧,比如我现在写的这篇博客,写完以后我要发表对吧? 发表这个过 ...
- 用SourceTree轻松Git项目图解
这篇文档的目的是:让使用Git更轻松. 看完这篇文档你能做到的是: 1.简单的用Git管理项目. 2.怎样既要开发又要处理发布出去的版本bug情况. SourceTree是一个免费的Git图形化管理工 ...
- NuGet 自定义配置
默认配置: 默认配置文件的路径%APPDATA%\NuGet\NuGet.Config (DOS) 或 $ENV:APPDATA\NuGet\NuGet.Config (PowerShell),(例如 ...
- 统计学习方法:罗杰斯特回归及Tensorflow入门
作者:桂. 时间:2017-04-21 21:11:23 链接:http://www.cnblogs.com/xingshansi/p/6743780.html 前言 看到最近大家都在用Tensor ...
- Angular2快速起步——构建一个简单的应用
构建此应用,分为如下几步: 1.环境准备:安装Node.js和npm: 2.创建并配置此项目: 3.创建应用: 4.创建组件并添加到应用程序中: 5.启动应用程序: 6.定义作为该应用的宿主页面: 7 ...