2012-03-10 57 views
0

如何刪除包含或不包含Java庫INI4J的部分?INI4J - 刪除部分

這不起作用

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

我還與ConfigParser試了一下。

回答

6

你的代碼不會做任何事情,絕對不會編譯。 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"))); 
+0

非常感謝。我讀過JavaDocs。我只是無法讓它去除一個部分。再次謝謝你。 – Verhelst 2012-03-11 11:16:29