ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

Java当中解析ini文件对应到JavaBean当中

2022-01-12 00:00:12  阅读:226  来源: 互联网

标签:Java String section value ini 当中 line property


目录

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,String,section,value,ini,当中,line,property
来源: https://blog.csdn.net/weixin_43888891/article/details/122444127

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有