项目需要INI配置文件读取修改,于是自己搞了个类,记录一下
public class ini {
private File file;
private HashMap<String,ArrayList<Item>> INI_Content = new HashMap<>();
public class Item {
private String Name;
private String Value;
public Item(String Name,String Value)
{
this.Name = Name;
this.Value = Value;
}
public String getName(){
return Name;
}
public String getValue(){
return Value;
}
public void SetValue(String val){
Value = val;
}
}
public ini(String ini_path){
try {
file = new File(ini_path);
if(!file.exists()) {
file.createNewFile();
}
BufferedReader bfr = new BufferedReader(new FileReader(file));
String readStr = "";
String Items_Name = null;
ArrayList<Item> content = new ArrayList<>();
boolean Add = false;
while((readStr = bfr.readLine()) != null)
{
if(readStr.indexOf("[") == 0 && readStr.lastIndexOf("]") == readStr.length() - 1)
{
if(Add)
{
ArrayList<Item> content_copy = new ArrayList<>();
INI_Content.put(Items_Name, content_copy);
for(int i = 0;i < content.size();i++)
{
Item item = content.get(i);
content_copy.add(item);
}
}
Items_Name = readStr.substring(1, readStr.length()-1);
if(!INI_Content.containsKey(Items_Name))
{
Add = true;
}else{
Add = false;
}
content.clear();
}else{
if(readStr.indexOf("=") != -1)
{
String[] a = readStr.split("=");
if(a.length == 2)
{
String Name = a[0];
String Value = a[1];
Item item = new Item(Name,Value);
content.add(item);
}
}
}
}
if(Add)
{
ArrayList<Item> content_copy = new ArrayList<>();
INI_Content.put(Items_Name, content_copy);
for(int i = 0;i < content.size();i++)
{
Item item = content.get(i);
content_copy.add(item);
}
}
bfr.close();
content.clear();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public ArrayList<Item> readItems(String tag)
{
if(INI_Content.containsKey(tag))
{
return INI_Content.get(tag);
}else{
return null;
}
}
public void WriteItem(String tag,HashMap<String,String> KeyValues)
{
if(INI_Content.containsKey(tag))
{
Iterator<Entry<String,String>> iter = KeyValues.entrySet().iterator();
ArrayList<Item> items_list = INI_Content.get(tag);
while(iter.hasNext())
{
boolean add = true;
Entry<String,String> entry = iter.next();
String key = entry.getKey();
String str_value = entry.getValue();
for(int i = 0;i < items_list.size();i++)
{
if(items_list.get(i).getName().equals(key))
{
items_list.get(i).SetValue(str_value);
add = false;
break;
}
}
if(add){
Item item = new Item(key,str_value);
items_list.add(item);
}
}
INI_Content.put(tag, items_list);
}else{
Iterator<Entry<String,String>> iter = KeyValues.entrySet().iterator();
ArrayList<Item> items_list = new ArrayList<>();
while(iter.hasNext())
{
Entry<String,String> entry = iter.next();
Item item = new Item(entry.getKey(),entry.getValue());
items_list.add(item);
}
INI_Content.put(tag, items_list);
}
StringBuffer sb = new StringBuffer();
Iterator<Entry<String,ArrayList<Item>>> iter = INI_Content.entrySet().iterator();
while(iter.hasNext())
{
Entry<String,ArrayList<Item>> session = iter.next();
sb.append("[" + session.getKey() + "]\r\n");
ArrayList<Item> items = session.getValue();
for(int i = 0;i<items.size();i++)
{
String name = items.get(i).getName();
String val = items.get(i).getValue();
sb.append(name+"="+val+"\r\n");
}
sb.append("\r\n");
}
try {
FileOutputStream fos = new FileOutputStream(file);
fos.write(sb.toString().getBytes());
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public HashMap<String, ArrayList<Item>> getINIContent()
{
return INI_Content;
}
}
使用
//读取配置
File inifile = new File(FilePath);
if (inifile.exists()) {
ini mini = new ini(FilePath);
ArrayList<ini.Item> cfglist = mini.readItems("main");
if (cfglist != null && cfglist.size() > 0)
{
/*
...
*/
}
else {
showToast("配置读取失败");
}
}
//保存配置到文件
ini mini = new ini(path);
HashMap<String,String> KeyValues = new HashMap<>();
KeyValues.put("ID","1");
KeyValues.put("name","wang");
mini.WriteItem("main",KeyValues);