[Java]Ini配置文件的读取
2017-02-02 11:01
441 查看
package reader; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.LinkedList; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * 读取配置信息 */ public class IniReader { private static IniReader cr = new IniReader(); private String defaultIniPath = "D:\\ShapeWar\\Config.ini"; File iniFile; private HashMap<String,IniVariable[]> hm; public static IniReader getIniReader() { return cr; } /** * 初始化类 */ public IniReader() { readerInitial(); } /** * 对配置文件的更改和读取 * @param section * @param variable * @return */ public String getValue(String section,String variable) { IniVariable[] iv = hm.get(section); return findValue(iv,variable).getData(); } /** * 更改配置文件 * @param section * @param variable * @param value */ public void setValue(String section,String variable,String value) { IniVariable[] iv = hm.get(section); findValue(iv,variable).setData(value); saveIni(); } /** * 配置文件初始化 */ private void readerInitial() { iniFile = new File(defaultIniPath); if(!iniFile.exists()) { System.out.println("错误!无配置信息!请检查配置文件是否完整!"); System.exit(0); } else loadIniFile(); } // private void showAll() // { // IniVariable[] i = hm.get("[选项]"); // for(int c = 0 ; c < i.length ; c++) // { // System.out.println(i[c].toString()); // } // } private IniVariable findValue(IniVariable[] iv , String variable) { for(int c = 0 ; c < iv.length ; c++) { if(iv[c].getVariable().equals(variable)) { return iv[c]; } } System.out.println("未找到"); return null; /** * 再最后调用,将改动的配置写入文件 */ private void saveIni() { ArrayList<String> sections = new ArrayList<String>(hm.keySet()); ArrayList<IniVariable[]> values = new ArrayList<IniVariable[]>(hm.values()); try { BufferedWriter bw = new BufferedWriter(new FileWriter(iniFile)); for(int c = 0 ; c < sections.size() ; c++) { bw.write(sections.get(c)+"\n"); for(int c1 = 0 ; c1 < values.get(c).length ; c1++) { bw.write(values.get(c)[c1].toString()+"\n"); } } bw.flush(); bw.close(); } catch (IOException e) {} } /** * 将加载ini文件所有配置,方便读取 */ private void loadIniFile() { hm = new HashMap<String,IniVariable[]>(); BufferedReader sectionReader; try { //一个IO类,用于遍历Ini文件 sectionReader = new BufferedReader(new FileReader(iniFile)); //用于存储Ini给节点下的配置信息 IniVariable[] iv = null; //节点名称 String section; //ini配置文件链表,能得到单个节点的全部配置信息 LinkedList<String[]> ill = new LinkedList<String[]>(); //Reader是否读取完整个文件,是则跳出循环 boolean isEnd = false; //初始化变量 String up = null; String down =null; //寻找节点需要的两个类 Pattern p = Pattern.compile("]"); Matcher m = null; //循环,遍历整个Ini文件 while(!isEnd) { if(up == null || down ==null)//初始化 { up = sectionReader.readLine().trim(); down = sectionReader.readLine().trim(); m = p.matcher(up); } while(!m.find())//寻找节,跳出循环后up为节所在行 { up = down; down = sectionReader.readLine(); m = p.matcher(up); } section = up; m = p.matcher(down); //添加保存单个节点的全部信息 while(!m.find()) { up = down; if(down.split("=").length>1) ill.add(down.split("=")); down = sectionReader.readLine(); //补充,读取到最后时提前结束循环 if(down == null) { isEnd = true; break; } m = p.matcher(down); } //保存节点信息到哈希表 iv = new IniVariable[ill.size()]; for(int c = 0 ; c<iv.length ; c++) { iv[c] = new IniVariable(ill.get(0)[0], ill.get(0)[1]); ill.removeFirst(); } hm.put(section, iv); //清楚单个链表的所有信息,循环利用 } //测试用 } catch (FileNotFoundException e) {} catch (IOException e) {} } /** * 内部类,用于存放单个节点的单个项的配置信息 * */ class IniVariable { private String variable; private String data; /** * * @param obj */ public IniVariable(String variable , String t) { this.variable = variable.trim(); data = t.trim(); } public void setData(String newData) { this.data = newData; } public String getData() { return data; } public String getVariable() { return variable; } public String toString() { return variable +"="+ data; } } }
之前在做java课设。。要写一个小游戏,读配置文档时在网上找了几种都不合心意,所以就自己写了一个,供大家参考,有的地方可能写麻烦了,但读取的各种信息无误,还有一个不足是读取不到相关项时候回抛异常我没有处理,所以读取的时候配置项注意不要写错了,另外配置项的"[ ]"两个括号也需要加上。
相关文章推荐
- Java读取和修改ini配置文件
- java读取和修改ini配置文件
- Java读取和修改ini配置文件
- java读取和修改ini配置文件
- Java_读取INI配置文件
- java读取和修改ini配置文件实例代码
- Java读取解析INI配置文件
- 使用java读取配置文件(ini、properties)
- Java 读取 INI 配置文件的方法
- java软件托盘MenuItem 显示中文出现框框乱码、读取ini配置文件乱码
- 2011-07-07 java读取ini配置文件
- java读取和修改ini配置文件
- 项目开发中经常要用到的知识----Java读取.Properties,.Xml,.Ini文件(二)
- 利用Java的Properties类读取配置文件信息
- ASP.NET 2.0 读取配置文件[INI](示例代码下载)
- ASP.NET 2.0 读取配置文件[INI](示例代码下载)
- java读取配置文件
- stl读取ini配置文件例子
- java读取配置文件的几种方法
- INI的替代品--XML配置文件读取与保存