zoukankan      html  css  js  c++  java
  • 使用IniEditor读写INI类型配置文件

    配置文件.ini格式

    INI文件由节、键、值组成。

    [section]

    参数(键=值)

    name=value

    注解

    注解使用分号表示(;)。在分号后面的文字,直到该行结尾都全部为注解。
    ; comment textINI文件的数据格式的例子(配置文件的内容) [Section1 Name]
    KeyName1=value1
    KeyName2=value2
    ...
    [Section2 Name]
    KeyName21=value21
    KeyName22=value22
    其中:
    [Section1 Name]用来表示一个段落。
    因为INI文件可能是项目中共用的,所以使用[Section Name]段名来区分不同用途的参数区。例如:[Section1 Name]表示传感器灵敏度参数区;[Section2 Name]表示测量通道参数区等等。
    KeyName1=value1 用来表示一个参数名和值。
    比如:
    7033=50
    7034=51
    其中:
    7033表示某传感器名,50表示它的灵敏度值。
    7034表示另一只传感器名,51表示它的灵敏度值。
     
    .ini 实例
    ; exp ini file
    [port]
    portname=COM4
    port=4
     

    读取ini配置文件代码示例

    ————————

    配置文件:users.ini

        [root]
        role = administrator
        last_login = 2003-05-04
    
        [joe]
        role = author
        last_login = 2003-05-13

    Java读取ini配置代码

    String root_role;
    String root_lastLogin;
    String joe_role;
    String joe_lastLogin;
    
    try {
        IniEditor inieditor = new IniEditor();
        inieditor.load("users.ini");
        root_role = inieditor.get("root", "role");
        root_lastLogin =  inieditor.get("root", "last_login");
        joe_role= inieditor.get("joe", "role");
        joe_lastLogin=  inieditor.get("joe", "last_login");
    } catch (IOException e) {
        e.printStackTrace();
    }

    Java修改ini配置代码

    import com.nikhaldimann.inieditor.IniEditor;
    
        IniEditor users = new IniEditor();
        users.load("users.ini");
        users.set("root", "last_login", "2003-05-16");//修改值
        users.addComment("root", "Must change password often");//给[section]最后一个元素添加注释
        users.set("root", "change_pwd", "10 days");
        users.addBlankLine("root");//在[section]结束部分增加一个换行
      users.save("users.ini");//保存

    修改之后的users.ini:

     [root]
        role = administrator
        last_login = 2003-05-16
    
        # Must change password often
        change_pwd = 10 days
    
        [joe]
        role = author
        last_login = 2003-05-13
  • 相关阅读:
    201141 live the lie until the lie becomes your life
    my php & mysql FAQ
    suger日料财务
    python 应用thrift thrift的监控fb303
    cherryPy学习
    my linux FAQ
    Javascript无阻塞加载方法
    设计模式学习笔记之组合模式模式
    【转】cookie
    C# 多线程
  • 原文地址:https://www.cnblogs.com/quyongjin/p/3124013.html
Copyright © 2011-2022 走看看