2012-03-10 4 views
0

Comment puis-je supprimer une section avec ou sans la bibliothèque Java INI4J?INI4J - Supprimer la section

Cela ne fonctionne pas

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

J'ai aussi essayé avec le ConfigParser.

Répondre

6

Votre code ne fait rien et ne compile définitivement pas. Wini n'est pas la bonne classe, d'après les documents ini4j, vous devez instancier un objet Ini et supprimer/créer des sections en utilisant l'objet Section. Je recommande fortement de lire la documentation Ini4J. Les tutoriels sont super et les exemples fournis répondent à votre question!

Bien que vous pouvez continuer à lire aussi ...

Compte tenu du fichier ini

[Section]

somekey = somevalue

somekey2 = somevalue2

somekey3 = somevalue3

(à l'aide Ini4J)

Nous pouvons écrire

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

Merci beaucoup. J'ai lu les JavaDocs. Je ne pouvais simplement pas le faire fonctionner pour enlever une section. Encore merci. – Verhelst

Questions connexes