Java解析ini文件

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

Java解析ini文件

怪咖软妹@   2022-05-23 我要评论

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当中不是什么问题了吧。

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们