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"));
}
}