.ini 文件是initialization file的缩写,即初始化文件,是windows的系统配置文件所采用的存储格式,统管windows的各项配置,ini文件也可以用来存放软件信息,在java开发当中有时候涉及到对接别的设备或者对接程序可能偶尔会遇到ini文件。
这个是我的一个ini文件。现在想要解析这个文件,对应到javabean当中,从下面可以看出,这个ini有两个节点,也可以认为是两个java对象,一个是patient,一个是report。
这个是已经写好的一个工具类,可以直接复制粘贴使用的,没有依赖任何第三方jar包,在工具类最下面有一个main方法,就是用法示例。
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 pariniutil { protected map<string,object> ctions = new hashmap<string,object>(); private transient string defaultname = "default"; private transient string ctionname; private transient properties property; private properties parentobj; /** * 构造函数 * * @param filename * 文件路径 * @throws ioexception */ public pariniutil(string filename) throws ioexception { bufferedreader reader = new bufferedreader(new filereader(filename)); read(reader); reader.clo(); } /** * 文件读取 * * @param reader * @throws ioexception */ protected void read(bufferedreader reader) throws ioexception { string line; ctionname = this.defaultname; property = new properties(); ctions.put(ctionname, property); while ((line = reader.readline()) != null) { parline(line); } } /** * 解析每行数据 * * @param line */ protected void parline(string line) { line = line.trim(); if (line.indexof('#') == 0 || line.indexof(';') == 0) { return; } if (line.matches("\\[.*\\]")) { ctionname = line.replacefirst("\\[(.*)\\]", "$1").trim(); property = new properties(); if (ctionname.matches(".*:.*")) { int pos = ctionname.indexof(':'); string child = ctionname.substring(0, pos); string parent = ctionname.substring(pos + 1); parentobj = this.getction(parent); if (parentobj != null) { property = (properties) parentobj.clone(); ctions.put(child, property); } } el { ctions.put(ctionname, property); } } el 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.tproperty(name, value); } } /** * 根据节 和 key 获取值 * * @param ction * @param key * @return string */ public string get(string ction, string key) { if (ction.equals(null) || ction == "") ction = this.defaultname; properties property = (properties) ctions.get(ction); if (property == null) { return null; } string value = property.getproperty(key); if (value == null) return null; return value; } /** * 获取节下所有key * * @param ction * @return properties 山西艺术学院 */ public properties getction(string ction) { if (ction.equals(null) || ction == "") ction = this.defaultname; properties property = (properties) ctions.get(ction); if (property == null) { ctions.put(ction, property红细胞的功能); } return property; } /** * 增加节点 及 值 * * @param ction */ public void t(string ction, string key, string value) { 内存不足怎么办 if (property == null) property = new properties(); if (ction.equals(null) || ction == "") ction = this.defaultname; if (key.equals(null) || key == "") { system.out.println("key is null"); return; } ctions.put(ction, property); property.tproperty(key, value); } /** * 增加节点 * * @param ction */ public void tction(string ction) { ctions.put(ction, property); } public static void main(string[] args) { string filename = "c:\\urs\\gxs\\desktop\\patient.ini"; pariniutil config = null; try { config = new pariniutil(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); }}有了这个应该想要解析ini对应到javabean当中不是什么问题了吧。
以上就是java实现解析ini文件对应到javabean中的详细内容,更多关于java解析ini文件的资料请关注www从军行王昌龄.887551.com其它相关文章!
本文发布于:2023-04-04 14:04:04,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/8051249df5d2f2b979bb9e0b8a8fbc59.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:Java实现解析ini文件对应到JavaBean中.doc
本文 PDF 下载地址:Java实现解析ini文件对应到JavaBean中.pdf
| 留言与评论(共有 0 条评论) |