自用ini配置文件类

项目需要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);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值