前言
在windows系统中常看到后缀名为ini的配置文件,我们可以用java程序来读取它,从而进行进一步的处理。
一、ini文件是什么?
1、ini文件简介
.ini 文件是Initialization File的缩写,即初始化文件,是windows的系统配置文件所采用的存储格式,统管windows的各项配置,一般用户就用windows提供的各项图形化管理界面就可实现相同的配置了。但在某些情况,还是要直接编辑ini才方便,一般只有很熟悉windows才能去直接编辑。
2、ini文件格式
[zdytext检查号.Label1]
name=Label1
width=66
height=20
left=0
top=2
forecolor=0
fontname=宋体
fontsize=12
caption=检查号:
fontbold=.f.
fontitalic=.f.
[zdytext检查号.TEXT1]
name=TEXT1
width=111
height=22
left=66
top=0
forecolor=0
fontname=宋体
fontsize=12
format=C
value=3371
fontbold=.f.
fontitalic=.f.
二、Java代码
1.引入maven依赖
<dependency>
<groupId>com.exp-blog</groupId>
<artifactId>javaini</artifactId>
<version>1.1.0</version>
<scope>compile</scope>
</dependency>
2.用到的工具类(直接复制可用)
代码如下(示例):
public class IniUtils {
/**
*
* @param urlPath
* @param t1 ini文件中的[] 汉字请转换成拼音
* @param t2 []下的参数key
* @return 返回参数 t1 + t2所对应的值 没找到则返回null
* @throws IOException
*/
public static String readValue(String urlPath, String t1, String t2) throws IOException {
IniFile iniFile = new BasicIniFile();
read(new File(urlPath), iniFile);
for (IniSection section : iniFile.getSections()) {
if(section.getName().equals(t1)){
for (IniItem item : section.getItems()) {
System.out.println(item.getName() + ":"+item.getValue());
if(item.getName().equals(t2)){
return item.getValue();
}
}
}
}
return null;
}
public static void read(File file, IniFile ini) throws IOException {
IniSection currentSection = null;
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "gbk"));
String comment = "";
Object lastCommentable = null;
String line;
while((line = reader.readLine()) != null) {
line = line.trim();
if (line.isEmpty()) {
if (!comment.isEmpty() && lastCommentable != null) {
((Commentable)lastCommentable).setPostComment(comment);
comment = "";
}
} else {
String itemName;
if (isComment(line)) {
itemName = line.substring(1).trim();
if (comment.isEmpty()) {
comment = itemName;
} else {
comment = comment + "\n" + itemName;
}
} else {
String itemValue;
if (isSection(line)) {
itemName = getSectionName(line);
itemValue = getEndLineComment(line);
if (ini.hasSection(itemName)) {
currentSection = ini.getSection(itemName);
} else {
try {
currentSection = ini.addSection(toPinyin(itemName));
}catch (Exception e){
}
}
currentSection.setEndLineComment(itemValue);
if (!comment.isEmpty()) {
currentSection.setPreComment(comment);
comment = "";
}
lastCommentable = currentSection;
} else if (isItem(line)) {
if (currentSection == null) {
throw new FormatException("An Item has been read,before any section.");
}
itemName = getItemName(line);
itemValue = getItemValue(line);
String endLineComment = getEndLineComment(line);
IniItem item;
if (currentSection.hasItem(itemName)) {
item = currentSection.getItem(itemName);
} else {
try {
item = currentSection.addItem(itemName);
} catch (InvalidNameException var11) {
throw new FormatException("The string \"" + itemName + "\" is an invalid name for an IniItem.");
}
}
item.setValue(itemValue);
item.setEndLineComment(endLineComment);
if (!comment.isEmpty()) {
item.setPreComment(comment);
comment = "";
}
lastCommentable = item;
}
}
}
}
if (!comment.isEmpty() && lastCommentable != null) {
((Commentable)lastCommentable).setPostComment(comment);
comment = "";
}
reader.close();
}
}
3、写个栗子
假如我们想读取途中标红的配置
代码如下
public static void main(String[] args) {
//第一个参数为ini的文件路径
String value = IniUtils.readValue
(FileUtils.getPath() + "fileStorage/thirdUpload/BCHAO/bchao.ini",
"zdytextjianchahao.TEXT1",
"value");
//运行后读取出value为“3371”
}