Java实现解析ini文件对应到JavaBean中

 更新时间:2022年01月12日 08:42:34   作者:怪咖软妹@  
ini 文件是Initialization File的缩写,即初始化文件,是windows的系统配置文件所采用的存储格式。这篇文章主要介绍了通过Java实现解析ini文件对应到JavaBean中,需要的可以参考一下

1、ini文件简介

.ini 文件是Initialization File的缩写,即初始化文件,是windows的系统配置文件所采用的存储格式,统管windows的各项配置,ini文件也可以用来存放软件信息,在java开发当中有时候涉及到对接别的设备或者对接程序可能偶尔会遇到ini文件。

2、ini文件

这个是我的一个ini文件。现在想要解析这个文件,对应到javabean当中,从下面可以看出,这个ini有两个节点,也可以认为是两个java对象,一个是PATIENT,一个是REPORT。

这个是已经写好的一个工具类,可以直接复制粘贴使用的,没有依赖任何第三方jar包,在工具类最下面有一个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;

    /**
     * 构造函数
     *
     * @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);
        }
    }

    /**
     * 解析每行数据
     *
     * @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);
        }
    }

    /**
     * 根据节 和 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;
    }

    /**
     * 获取节下所有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;
    }

    /**
     * 增加节点 及 值
     *
     * @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);
    }

    /**
     * 增加节点
     *
     * @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、示例运行结果

有了这个应该想要解析ini对应到javabean当中不是什么问题了吧。

以上就是Java实现解析ini文件对应到JavaBean中的详细内容,更多关于Java解析ini文件的资料请关注脚本之家其它相关文章!

相关文章

  • spring data jpa如何使用自定义repository实现类

    spring data jpa如何使用自定义repository实现类

    这篇文章主要介绍了spring data jpa如何使用自定义repository实现类,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-11-11
  • java8中Stream的使用示例教程

    java8中Stream的使用示例教程

    Stream是Java8的一大亮点,是对容器对象功能的增强,下面这篇文章主要给大家介绍了关于java8中Stream使用的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2018-10-10
  • 详细总结Java组合模式

    详细总结Java组合模式

    今天带大家了解Java设计模式中的组合模式,下文中对组合模式介绍的非常详细,还有相关代码,对正在学习Java的小伙伴们很有帮助,需要的朋友可以参考下
    2021-05-05
  • Spring-cloud Feign 的深入理解

    Spring-cloud Feign 的深入理解

    这篇文章主要介绍了Spring-cloud Feign 的深入理解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2019-02-02
  • java仿百度假分页代码实现的详解

    java仿百度假分页代码实现的详解

    这篇文章主要介绍了java仿百度假分页代码实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-04-04
  • Java BufferedWriter BufferedReader 源码分析

    Java BufferedWriter BufferedReader 源码分析

    本文是关于Java BufferedWriter ,BufferedReader 简介、分析源码 对Java IO 流深入了解,希望看到的同学对你有所帮助
    2016-07-07
  • 详解mybatis多对一关联查询的方式

    详解mybatis多对一关联查询的方式

    这篇文章主要给大家介绍了关于mybatis多对一关联查询的相关资料,文中将关联方式以及配置方式介绍的很详细,需要的朋友可以参考下
    2021-06-06
  • spring boot中controller的使用及url参数的获取方法

    spring boot中controller的使用及url参数的获取方法

    这篇文章主要介绍了spring boot中controller的使用及url参数的获取方法,非常不错,具有参考借鉴价值,需要的朋友可以参考下
    2018-01-01
  • 深入了解Java定时器中的Timer的原理

    深入了解Java定时器中的Timer的原理

    这篇文章主要介绍了Java定时器中的Timer的原理。Timer主要用于Java线程里指定时间或周期运行任务,它是线程安全的,但不提供实时性(real-time)保证。接下来就跟随小编一起深入了解Timer吧
    2022-01-01
  • java中参数传递方式详解

    java中参数传递方式详解

    这篇文章主要介绍了java中参数传递方式详解的相关资料,需要的朋友可以参考下
    2017-03-03

最新评论