java读取ini文件

java读取ini文件 实现如下功能:
(1)从 ini文件中读取 ini属性值
(2)根据标题分别存储属性值
(3)支持空格,空行处理

在这里插入图片描述



package Tools.File;

import java.io.*;
import java.util.*;
import java.util.function.Predicate;

public class ReadINIfile {

    public static void main(String[] args) throws IOException {

        //测试
        ArrayList<String> fileconten=ReadINIfile.readFileContent("E:\\ZKN\\JavaLesson\\SXJAVA\\src\\Tools\\File\\band.ini");
        HashMap<String, HashMap<String, String>>  allsections = parseAllsection(fileconten);
        for(String s:allsections.keySet())
        {
            System.out.println("----"+s+"---");
            System.out.println("band= "+allsections.get(s).get("band"));
            System.out.println("Start= "+allsections.get(s).get("start"));
            System.out.println("stop= "+allsections.get(s).get("stop"));
            System.out.println("arfcnmin= "+allsections.get(s).get("arfcnmin"));
            System.out.println("arfcnmax= "+allsections.get(s).get("arfcnmax"));
        }
    }


    /***
     * 从文件内容中解析 ini内容,返回属性
     * @param fileconten
     * @return HashMap<标题, HashMap<属性, 属性值> >
     */
    public static  HashMap<String,HashMap<String,String> > parseAllsection(ArrayList<String> fileconten ) {
        //得到所有的标题
        List<String> titleList = new ArrayList<>();
        for (String line : fileconten) {
            if (line.contains("[")) {

                titleList.add(line.trim());
            }

        }
        HashMap<String, List<String>> sections = new HashMap<>();
        for (int t = 0; t < titleList.size(); t++) {
            int sectionStart = 0;
            int sectionStop = 0;

            //获取section内容开始位置 :在  标题的下一行
            sectionStart = fileconten.indexOf(titleList.get(t)) + 1;

            //获取section内容结束位置 :在  下一个标题的前一行,或者文件内容结尾
            if (t == (titleList.size() - 1)) {
                sectionStop = fileconten.size()-1;
            } else {
                sectionStop = fileconten.indexOf(titleList.get(t + 1)) - 1;
            }

            //截取元素 注意subList ,stop元素不含因此一定要到下一个
            List<String> temp = fileconten.subList(sectionStart, sectionStop+1);

        //移除空行 ,直接操作ArrayList 元素回出现异常,移动到读取文件时候就删除空行
//          temp.removeIf(new Predicate<String>() {
//              @Override
//              public boolean test(String s) {
//                  if (s.isEmpty()) {
//                      return true;
//                  } else {
//                      return false;
//                  }
//              }
//          });

//            Iterator<String> iterator=temp.iterator();
//            while (iterator.hasNext())
//            {
//                if((iterator.next()).equals(""))
//                {
//                    iterator.remove();
//                }
//            }


                sections.put(titleList.get(t), temp);




        }


        HashMap<String, HashMap<String, String>> sectionValue = new HashMap<>();
        for (String s : sections.keySet()) {

            List<String> dataline = sections.get(s);
            HashMap<String, String> h1 = new HashMap<>();
            //取出section 的每一行,按#拆分组成HashMapmap
            for (String s1 : dataline) {
                String[] Key_value = s1.split("=");
                h1.put(Key_value[0].trim(), Key_value[1].trim());
            }
            //将组成的HashMapmap 再次组装成为最终的HashMapmap ---HashMap<String,HashMap<String,String> > sectionValue
            sectionValue.put(s, h1);
        }
        return sectionValue;
    }


    /***
     *  按行读取文件内容 ,并删除空行
     * @param filename :需要读取的文件名
     * @return   文件内容list,每行为一个元素
     * @throws IOException
     */
    public static ArrayList<String> readFileContent(String filename) throws IOException {

        FileReader fileReader=new FileReader(filename);
        BufferedReader bufferedReader=new BufferedReader(fileReader);
        String temp;
        ArrayList<String> content=new ArrayList<>();
        while( (temp=bufferedReader.readLine() )!=null   )
        {
            if(!temp.isEmpty()) {
                content.add(temp);
            }
        }
        return  content;
    }
}



在这里插入图片描述

  • Java 自带的 Properties 类也有读取ini 文件功能,不过功能单一,实例如下
package TestIO;

import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;

public class ReadINI {

    public static void main(String[] args) throws IOException {

        Properties properties=new Properties();
        FileReader fileReader=new FileReader("C:\\EIKZHAA\\java\\codespace\\MyTest\\src\\TestIO\\info.ini");
        properties.load(fileReader);
        System.out.println( properties.get("name"));
        System.out.println( properties.get("age"));

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值