I’m trying to save a .properties file with entries sorted alphabetically using this class:
SortedProperties
Like this:
File file = new File(fullPathToCfgFile);
FileOutputStream os = new FileOutputStream(file);
getProperties().store(os, null);
os.close();
private static SortedProperties getProperties() {
SortedProperties properties = new SortedProperties();
Field[] fields = Settings.class.getDeclaredFields();
for (int i = 0; i < fields.length; i++) {
String fieldName = fields[i].getName();
if (fieldName.startsWith("_")) {
try {
properties.put(fieldName, "" + fields[i].get(null));
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
return properties;
}
The problem is keys() from SortedProperties is never called, so the saved file is not sorted.
I tested your code and
keys()is definitely invoked bystore().I don’t know what issues you are encountering, but it doesn’t look to be in
SortedProperties.