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
  • 相关阅读:
    #特征方程,dp,快速幂#洛谷 4451 [国家集训队]整数的lqp拆分
    #状压dp,贪心#CF1316E Team Building
    #线段树,欧拉函数#CF1114F Please, another Queries on Array?
    #启发式合并,链表#洛谷 3201 [HNOI2009] 梦幻布丁
    #树状数组#洛谷 4113 [HEOI2012]采花
    #链表#洛谷 3794 签到题IV
    #矩阵乘法,斐波那契#洛谷 2544 [AHOI2004] 数字迷阵
    #dp#洛谷 4399 [JSOI2008]Blue Mary的职员分配
    #同余最短路#洛谷 3403 跳楼机
    #网络流,分层图#洛谷 4400 [JSOI2008] Blue Mary的旅行
  • 原文地址:https://www.cnblogs.com/quyongjin/p/3124013.html
Copyright © 2011-2022 走看看