【问题标题】:INI4J - Delete SectionINI4J - 删除部分
【发布时间】:2012-03-10 23:32:31
【问题描述】:

如何删除带有或不带有 Java 库 INI4J 的部分?

这不起作用

Wini ini = new Wini(File);
System.out.println(Integer.toString(Position));
ini.remove(Integer.toString(Position));

我也尝试使用 ConfigParser。

【问题讨论】:

    标签: java ini4j


    【解决方案1】:

    您的代码不执行任何操作,并且绝对不会编译。 Wini 不是正确的类,根据 ini4j 文档,您需要实例化一个 Ini 对象并使用 Section 对象删除/创建部分。

    我强烈建议您通读Ini4J 文档。教程很棒,提供的示例回答了您的问题!

    虽然你也可以继续阅读……

    给定 Ini 文件

    [部分]

    somekey = somevalue

    somekey2 = somevalue2

    somekey3 = somevalue3

    (使用 Ini4J)

    我们可以写

    Ini iniFile = new Ini(new FileInputStream(new File("/path/to/the/ini/file.ini")));
    /*
     * Removes a key/value you pair from a section
     */
    // Check to ensure the section exists
    if (iniFile.containsKey("Section")) { 
        // Get the section, a section contains a Map<String,String> of all associated settings
        Section s = iniFile.get("Section");
    
        // Check for a given key<->value mapping
        if ( s.containsKey("somekey") ) { 
            // remove said key<->value mapping
            s.remove("somekey");
        }
    }
    
    /*
     * Removes an entire section
     */
    if (iniFile.containsKey("Section")) { 
        // Gets the section and removes it from the file
        iniFile.remove(iniFile.get("Section"));
    }
    
    // Store our changes back out into the file
    iniFile.store(new FileOutputStream(new File("/path/to/the/ini/file.ini")));
    

    【讨论】:

    • 非常感谢。我确实阅读了 JavaDocs。我只是无法让它删除一个部分。再次感谢您。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-23
    • 1970-01-01
    • 2011-02-10
    相关资源
    最近更新 更多