自制的一个 ini 文件读写对象

发表于 2020-05-13 18:08:00
阅读 27

介绍

介绍

开发程序需要操作 ini 文件,发现内建库没有对应对象,无奈之下自己捣鼓一个吧。。。

教程

IniFileSection

通过 Map<String, String> 保存用户设置参数

class IniFileSection{
    private Map<String, String> items;

    public IniFileSection(){
        items = new HashMap<String, String>();
    }

    public void SetValue(String key, String value){
        items.put(key, value);
    }

    public void RemoveValue(String key){
        items.remove(key);
    }

    public String GetValue(String key){

        return items.get(key);
    }

    public Map<String, String> GetValues(){

        return items;
    }
}

IniFile

通过 Map<String, IniFileSection> 保存多段用户设置参数集合

无 section 的用户设置参数会存到 sectionKey 为空的特殊 section 里面

public class IniFile{

    private String fileAbsPath;
    private String lastErrorMessage;
    private FileOutputStream fileWriter;
    private FileInputStream fileReader;
    private Map<String, IniFileSection> sections;

    public IniFile(String uFileAbsPath){
        fileAbsPath = uFileAbsPath;
        lastErrorMessage = "";
        sections = new HashMap<String, IniFileSection>();
    }

    public void SetValue(String sectionKey, String key, String value){
        IniFileSection section;

        // get section
        section = sections.get(sectionKey);
        if(section == null){
            section = new IniFileSection();
            sections.put(sectionKey, section);
        }

        // set value
        section.SetValue(key, value);
    }

    public void RemoveValue(String sectionKey, String key, String value){
        IniFileSection section;

        // get section
        section = sections.get(sectionKey);
        if(section == null){
            section = new IniFileSection();
            sections.put(sectionKey, section);
        }

        // set value
        section.RemoveValue(key);
    }

    public String GetValue(String sectionKey, String key){
        IniFileSection section;
        String value;

        // get section
        section = sections.get(sectionKey);

        // get value
        if(section != null){
            value = section.GetValue(key);

            return value;
        }

        return null;
    }

    public void Load(){
        try{
            BufferedReader reader = new BufferedReader(new FileReader(fileAbsPath));
            String line;
            String secionKey;
            IniFileSection section;
            section = new IniFileSection();
            sections.put("", section);
            while((line = reader.readLine()) != null){
                line = line.trim();
                if (line.matches("\\[.*\\]") == true) {
                    secionKey = line.replaceFirst("\\[(.*)\\]", "$1");
                    section = new IniFileSection();
                    sections.put(secionKey, section);
                }
                else if (line.matches(".*=.*") == true) {
                    if (section != null) {
                        int i = line.indexOf('=');
                        String name = line.substring(0, i);
                        String value = line.substring(i + 1);
                        section.SetValue(name, value);
                    }
                }
            }
            reader.close();
        }
        catch (Exception e){
            lastErrorMessage = e.getMessage();
        }
    }

    public void Save(){
        try {
            fileWriter = new FileOutputStream(fileAbsPath);
            String line;
            for (Map.Entry<String, IniFileSection> entry : sections.entrySet()) {
                line = String.format("[%s]", entry.getKey()) + "\n";
                fileWriter.write(line.getBytes());
                IniFileSection section = entry.getValue();
                for (Map.Entry<String, String> subEntry : section.GetValues().entrySet()) {
                    line = String.format("%s=%s", subEntry.getKey(), subEntry.getValue()) + "\n";
                    fileWriter.write(line.getBytes());
                }
            }
            fileWriter.flush();
            fileWriter.close();
        }
        catch (Exception e){
            lastErrorMessage = e.getMessage();
        }
    }

    public String GetLastError(){

        return lastErrorMessage;
    }
}

总结

主要就是通过 IO 的读写操作和两个 Map 结构完成了对 ini 文件的读写操作