2023年12月2日发(作者:)

Java实现解析ini文件对应到JavaBean中目录1、ini文件简介2、ini文件3、ini解析工具类4、示例运行结果1、ini文件简介.ini 文件是Initialization File的缩写,即初始化文件,是windows的系统配置文件所采用的存储格式,统管windows的各项配置,ini文件也可以用来存放软件信息,在java开发当中有时候涉及到对接别的设备或者对接程序可能偶尔会遇到ini文件。2、ini文件这个是我的一个ini文件。现在想要解析这个文件,对应到javabean当中,从下面可以看出,这个ini有两个节点,也可以认为是两个java对象,一个是PATIENT,一个是REPORT。这个是已经写好的一个工具类,可以直接复制粘贴使用的,没有依赖任何第三方jar包,在工具类最下面有一个main方法,就是用法示例。3、ini解析工具类import edReader;import ader;import ption;import p;import ;import ties;public class ParseIniUtil { protected Map sections = new HashMap(); private transient String defaultName = "default"; private transient String sectionName; private transient Properties property; private Properties parentObj; /** * 构造函数 * * @param filename * 文件路径 * @throws IOException */ public ParseIniUtil(String filename) throws IOException { BufferedReader reader = new BufferedReader(new FileReader(filename)); read(reader); (); } /** * 文件读取 * * @param reader * @throws IOException */ protected void read(BufferedReader reader) throws IOException { String line; sectionName = tName; property = new Properties(); (sectionName, property); while ((line = ne()) != null) { parseLine(line); } } /** * 解析每行数据 * * @param line */ protected void parseLine(String line) { line = (); if (f('#') == 0 || f(';') == 0) { return; } if (s("[.*]")) { sectionName = eFirst("[(.*)]", "$1").trim(); property = new Properties(); if (s(".*:.*")) { int pos = f(':'); String child = ing(0, pos); String parent = ing(pos + 1); parentObj = tion(parent); if (parentObj != null) { property = (Properties) (); (child, property); } } else { (sectionName, property); } } else if (s(".*=.*")) { int i = f('='); String name = ing(0, i).trim(); String value = ing(i + 1).trim(); if (f('"') == 0 || f(''') == 0) { // 去掉前面符号 " 或 ' value = ing(1, ()); // 去掉后面 " 或 ' int len = (); if (f('"') == len - 1 || f(''') == len - 1) { value = ing(0, len - 1); } } perty(name, value); } } /** * 根据节 和 key 获取值 * * @param section * @param key * @return String */ public String get(String section, String key) { if ((null) || section == "") section = tName; Properties property = (Properties) (section); if (property == null) { return null; } String value = perty(key); if (value == null) return null; return value; } /** * 获取节下所有key * * @param section * @return Properties */ public Properties getSection(String section) { if ((null) || section == "") section = tName; Properties property = (Properties) (section); if (property == null) { (section, property); } return property; } /** * 增加节点 及 值 * * @param section */ public void set(String section, String key, String value) { if (property == null) property = new Properties(); if ((null) || section == "") section = tName; if ((null) || key == "") { n("key is null"); return; } (section, property); perty(key, value); } /** * 增加节点 * * @param section */ public void setSection(String section) { (section, property); } public static void main(String[] args) { String fileName = "C:"; ParseIniUtil config = null; try { config = new ParseIniUtil(fileName); } catch (IOException e) { tackTrace(); } String app = ("PATIENT", "OrderID"); n("OrderID = " + app); String app1 = ("REPORT", "PostCode"); n("PostCode = " + app1); }}4、示例运行结果有了这个应该想要解析ini对应到javabean当中不是什么问题了吧。以上就是Java实现解析ini文件对应到JavaBean中的详细内容,更多关于Java解析ini文件的资料请关注其它相关文章!