用Java读取.ini配置文件


前言

在windows系统中常看到后缀名为ini的配置文件,我们可以用java程序来读取它,从而进行进一步的处理。


一、ini文件是什么?

1、ini文件简介

.ini 文件是Initialization File的缩写,即初始化文件,是windows的系统配置文件所采用的存储格式,统管windows的各项配置,一般用户就用windows提供的各项图形化管理界面就可实现相同的配置了。但在某些情况,还是要直接编辑ini才方便,一般只有很熟悉windows才能去直接编辑。

2、ini文件格式

[zdytext检查号.Label1]
name=Label1
width=66
height=20
left=0
top=2
forecolor=0
fontname=宋体
fontsize=12
caption=检查号:
fontbold=.f.
fontitalic=.f.
[zdytext检查号.TEXT1]
name=TEXT1
width=111
height=22
left=66
top=0
forecolor=0
fontname=宋体
fontsize=12
format=C
value=3371
fontbold=.f.
fontitalic=.f.

二、Java代码

1.引入maven依赖

<dependency>
            <groupId>com.exp-blog</groupId>
            <artifactId>javaini</artifactId>
            <version>1.1.0</version>
            <scope>compile</scope>
 </dependency>

2.用到的工具类(直接复制可用)

代码如下(示例):

public class IniUtils {
	/**
     *
     * @param urlPath
     * @param t1 ini文件中的[] 汉字请转换成拼音
     * @param t2 []下的参数key
     * @return 返回参数 t1 + t2所对应的值 没找到则返回null
     * @throws IOException
     */
    public static String readValue(String urlPath, String t1, String t2) throws IOException {
        IniFile iniFile = new BasicIniFile();
        read(new File(urlPath), iniFile);
        for (IniSection section : iniFile.getSections()) {
            if(section.getName().equals(t1)){
                for (IniItem item : section.getItems()) {
                    System.out.println(item.getName() + ":"+item.getValue());
                    if(item.getName().equals(t2)){
                        return item.getValue();
                    }
                }
            }
        }
        return null;
    }

public static void read(File file, IniFile ini) throws IOException {
        IniSection currentSection = null;
        BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "gbk"));
        String comment = "";
        Object lastCommentable = null;

        String line;
        while((line = reader.readLine()) != null) {
            line = line.trim();
            if (line.isEmpty()) {
                if (!comment.isEmpty() && lastCommentable != null) {
                    ((Commentable)lastCommentable).setPostComment(comment);
                    comment = "";
                }
            } else {
                String itemName;
                if (isComment(line)) {
                    itemName = line.substring(1).trim();
                    if (comment.isEmpty()) {
                        comment = itemName;
                    } else {
                        comment = comment + "\n" + itemName;
                    }
                } else {
                    String itemValue;
                    if (isSection(line)) {
                        itemName = getSectionName(line);
                        itemValue = getEndLineComment(line);
                        if (ini.hasSection(itemName)) {
                            currentSection = ini.getSection(itemName);
                        } else {
                            try {

                                currentSection = ini.addSection(toPinyin(itemName));
                            }catch (Exception e){

                            }
                        }

                        currentSection.setEndLineComment(itemValue);
                        if (!comment.isEmpty()) {
                            currentSection.setPreComment(comment);
                            comment = "";
                        }

                        lastCommentable = currentSection;
                    } else if (isItem(line)) {
                        if (currentSection == null) {
                            throw new FormatException("An Item has been read,before any section.");
                        }

                        itemName = getItemName(line);
                        itemValue = getItemValue(line);
                        String endLineComment = getEndLineComment(line);
                        IniItem item;
                        if (currentSection.hasItem(itemName)) {
                            item = currentSection.getItem(itemName);
                        } else {
                            try {
                                item = currentSection.addItem(itemName);
                            } catch (InvalidNameException var11) {
                                throw new FormatException("The string \"" + itemName + "\" is an invalid name for an IniItem.");
                            }
                        }

                        item.setValue(itemValue);
                        item.setEndLineComment(endLineComment);
                        if (!comment.isEmpty()) {
                            item.setPreComment(comment);
                            comment = "";
                        }

                        lastCommentable = item;
                    }
                }
            }
        }

        if (!comment.isEmpty() && lastCommentable != null) {
            ((Commentable)lastCommentable).setPostComment(comment);
            comment = "";
        }

        reader.close();
    }
}

3、写个栗子

假如我们想读取途中标红的配置

在这里插入图片描述
代码如下

  public static void main(String[] args) {
		  //第一个参数为ini的文件路径
        String value = IniUtils.readValue
        (FileUtils.getPath() + "fileStorage/thirdUpload/BCHAO/bchao.ini", 
        "zdytextjianchahao.TEXT1", 
        "value");
        //运行后读取出value为“3371”
    }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值