如何删除包含或不包含Java库INI4J的部分?
这不起作用
Wini ini = new Wini(File);
System.out.println(Integer.toString(Position));
ini.remove(Integer.toString(Position));
Run Code Online (Sandbox Code Playgroud)
我也尝试使用ConfigParser.
你的代码没有做任何事情,绝对不会编译.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")));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2003 次 |
| 最近记录: |