0
0
ProfileReader.zip
Les espaces sont tolérés entre la clé et le =, et à l'intérieur de la valeur. Les lignes commençant par un ; sont considérées comme des commentaires ..
Toutes les valeurs doivent appartenir à une section
Bonjour,
Dans un fichier ini, il est fréquent de ne pas renseigner de valeurs (mot de passe, par exemple, s'il n'est pas utilisé - cas d'une base de données embarquée).
Voici la correction qui permet de gérer correctement ce cas de figure :
[private void addLineToSection(String aLine,
Hashtable
if (null == aLine) {
return;
}
if (null == aSection) {
throw new Exception("No section found to add data");
}
aLine = aLine.trim();
// lines that starts with ; are comments
if (aLine.startsWith(";")) {
return;
}
// Avoid the empty lines
if (aLine.length() == 0) {
return;
}
if (aLine.endsWith("=")) {
}
// The format of a line of data is: key = value
StringTokenizer st = new StringTokenizer(aLine, "=");
String key = "";
String value = "";
// The value is empty
if (aLine.endsWith("=")) {
key = st.nextToken().trim();
} else {
// the value is defining
if (st.countTokens() != 2) {
throw new Exception("Invalid format of data: " + aLine);
}
key = st.nextToken().trim();
// a key should not contain spaces
for (int index = 0; index < key.length(); index++) {
if (Character.isWhitespace(key.charAt(index))) {
throw new Exception("Invalid format of data: " + aLine);
}
}
value = st.nextToken().trim();
}
aSection.put(key, value);
}