INI4J - 删除部分
问题描述:
如何删除包含或不包含Java库INI4J的部分?INI4J - 删除部分
这不起作用
Wini ini = new Wini(File);
System.out.println(Integer.toString(Position));
ini.remove(Integer.toString(Position));
我还与ConfigParser试了一下。
答
你的代码不会做任何事情,绝对不会编译。 Wini不是正确的类,根据需要实例化Ini对象并使用Section对象移除/创建节的ini4j文档。
我强烈建议通过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。我只是无法让它去除一个部分。再次谢谢你。 – Verhelst 2012-03-11 11:16:29