您的位置:首页 > 编程语言 > Java开发

[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课设。。要写一个小游戏,读配置文档时在网上找了几种都不合心意,所以就自己写了一个,供大家参考,有的地方可能写麻烦了,但读取的各种信息无误,还有一个不足是读取不到相关项时候回抛异常我没有处理,所以读取的时候配置项注意不要写错了,另外配置项的"[ ]"两个括号也需要加上。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: