Java實現(xiàn)解析ini文件對應(yīng)到JavaBean中
1、ini文件簡介
.ini 文件是Initialization File的縮寫,即初始化文件,是windows的系統(tǒng)配置文件所采用的存儲格式,統(tǒng)管windows的各項配置,ini文件也可以用來存放軟件信息,在java開發(fā)當中有時候涉及到對接別的設(shè)備或者對接程序可能偶爾會遇到ini文件。
2、ini文件
這個是我的一個ini文件?,F(xiàn)在想要解析這個文件,對應(yīng)到j(luò)avabean當中,從下面可以看出,這個ini有兩個節(jié)點,也可以認為是兩個java對象,一個是PATIENT,一個是REPORT。

這個是已經(jīng)寫好的一個工具類,可以直接復(fù)制粘貼使用的,沒有依賴任何第三方j(luò)ar包,在工具類最下面有一個main方法,就是用法示例。
3、ini解析工具類
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
public class ParseIniUtil {
protected Map<String,Object> sections = new HashMap<String,Object>();
private transient String defaultName = "default";
private transient String sectionName;
private transient Properties property;
private Properties parentObj;
/**
* 構(gòu)造函數(shù)
*
* @param filename
* 文件路徑
* @throws IOException
*/
public ParseIniUtil(String filename) throws IOException {
BufferedReader reader = new BufferedReader(new FileReader(filename));
read(reader);
reader.close();
}
/**
* 文件讀取
*
* @param reader
* @throws IOException
*/
protected void read(BufferedReader reader) throws IOException {
String line;
sectionName = this.defaultName;
property = new Properties();
sections.put(sectionName, property);
while ((line = reader.readLine()) != null) {
parseLine(line);
}
}
/**
* 解析每行數(shù)據(jù)
*
* @param line
*/
protected void parseLine(String line) {
line = line.trim();
if (line.indexOf('#') == 0 || line.indexOf(';') == 0) {
return;
}
if (line.matches("\\[.*\\]")) {
sectionName = line.replaceFirst("\\[(.*)\\]", "$1").trim();
property = new Properties();
if (sectionName.matches(".*:.*")) {
int pos = sectionName.indexOf(':');
String child = sectionName.substring(0, pos);
String parent = sectionName.substring(pos + 1);
parentObj = this.getSection(parent);
if (parentObj != null) {
property = (Properties) parentObj.clone();
sections.put(child, property);
}
} else {
sections.put(sectionName, property);
}
} else if (line.matches(".*=.*")) {
int i = line.indexOf('=');
String name = line.substring(0, i).trim();
String value = line.substring(i + 1).trim();
if (value.indexOf('"') == 0 || value.indexOf('\'') == 0) {
// 去掉前面符號 " 或 '
value = value.substring(1, value.length());
// 去掉后面 " 或 '
int len = value.length();
if (value.indexOf('"') == len - 1 || value.indexOf('\'') == len - 1) {
value = value.substring(0, len - 1);
}
}
property.setProperty(name, value);
}
}
/**
* 根據(jù)節(jié) 和 key 獲取值
*
* @param section
* @param key
* @return String
*/
public String get(String section, String key) {
if (section.equals(null) || section == "")
section = this.defaultName;
Properties property = (Properties) sections.get(section);
if (property == null) {
return null;
}
String value = property.getProperty(key);
if (value == null)
return null;
return value;
}
/**
* 獲取節(jié)下所有key
*
* @param section
* @return Properties
*/
public Properties getSection(String section) {
if (section.equals(null) || section == "")
section = this.defaultName;
Properties property = (Properties) sections.get(section);
if (property == null) {
sections.put(section, property);
}
return property;
}
/**
* 增加節(jié)點 及 值
*
* @param section
*/
public void set(String section, String key, String value) {
if (property == null)
property = new Properties();
if (section.equals(null) || section == "")
section = this.defaultName;
if (key.equals(null) || key == "") {
System.out.println("key is null");
return;
}
sections.put(section, property);
property.setProperty(key, value);
}
/**
* 增加節(jié)點
*
* @param section
*/
public void setSection(String section) {
sections.put(section, property);
}
public static void main(String[] args) {
String fileName = "C:\\Users\\gxs\\Desktop\\Patient.ini";
ParseIniUtil config = null;
try {
config = new ParseIniUtil(fileName);
} catch (IOException e) {
e.printStackTrace();
}
String app = config.get("PATIENT", "OrderID");
System.out.println("OrderID = " + app);
String app1 = config.get("REPORT", "PostCode");
System.out.println("PostCode = " + app1);
}
}
4、示例運行結(jié)果

有了這個應(yīng)該想要解析ini對應(yīng)到j(luò)avabean當中不是什么問題了吧。
以上就是Java實現(xiàn)解析ini文件對應(yīng)到JavaBean中的詳細內(nèi)容,更多關(guān)于Java解析ini文件的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Spring ApplicationListener監(jiān)聽器用法詳解
這篇文章主要介紹了Spring ApplicationListener監(jiān)聽器用法詳解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-11-11
使用@TableField(updateStrategy=FieldStrategy.IGNORED)遇到的坑記錄
這篇文章主要介紹了使用@TableField(updateStrategy=FieldStrategy.IGNORED)遇到的坑及解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-11-11
SpringSecurity?默認登錄認證的實現(xiàn)原理解析
這篇文章主要介紹了SpringSecurity?默認登錄認證的實現(xiàn)原理解析,本文通過圖文示例相結(jié)合給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-12-12
Java從控制臺讀入數(shù)據(jù)的幾種方法總結(jié)
下面小編就為大家?guī)硪黄狫ava從控制臺讀入數(shù)據(jù)的幾種方法總結(jié)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-10-10
Spring?Security+JWT如何實現(xiàn)前后端分離權(quán)限控制
本篇將手把手教你用?Spring?Security?+?JWT?搭建一套完整的登錄認證與權(quán)限控制體系,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2025-04-04
Spring?Cloud?通過?Gateway?webflux實現(xiàn)網(wǎng)關(guān)異常處理
在某一個服務(wù)中出現(xiàn)異常,通過@ControllerAdvice?+?@ExceptionHandler?統(tǒng)一異常處理,即使在微服務(wù)架構(gòu)中,也可以將上述統(tǒng)一異常處理放入到公共的微服務(wù)中,這樣哪一個微服務(wù)需要,直接引入模塊,本文重點介紹Spring?Cloud?通過?Gateway?webflux實現(xiàn)網(wǎng)關(guān)異常處理,一起看看吧2023-11-11
Java中static關(guān)鍵字的作用和用法詳細介紹
這篇文章主要介紹了Java中static關(guān)鍵字的作用和用法詳細介紹,本文講解了static變量、靜態(tài)方法、static代碼塊、static和final一塊用等內(nèi)容,需要的朋友可以參考下2015-01-01

