I was trying to use java's native Properties class to edit my ".properties" file and save them, but then I was put off by the fact that in order to do that I had to "store" the properties file by re-writing it back to disk, which simply erased all other properties (say if i had three properties and i edited one of them, the store method erases the other two). Someone directed me to the Apache Commons, and at first glance I was impressed because it seemed all i had to do was call the "save()" method. However, using Apache commons has been nothing short of frustrating, to say the least. Perhaps one of you out there can help me out.
First of, when loading a file it seems I cannot do this:
PropertiesConfiguration configProp = new PropertiesConfiguration();
configProp.load("properties/configurations.properties");
When I try the code above all I get is an error that says "The File has not been set!". Apparently, if I am to use the "load()" method, I am to do this:
PropertiesConfiguration configProp = new PropertiesConfiguration();
configProp.setFileName("testing.properties");
configProp.load();
Which makes me wonder: why on earth does the load() method allow for the inclusion of a properties file's path it doesn't work????
The only other way to load a properties file is this:
PropertiesConfiguration configProp = new PropertiesConfiguration("properties/configurations.properties");
Honestly, I do not want to have to instantiate a properties configuration object everytime I want to load a properties file. It just seems too messy ... and a bit daft.
Also, say, within my properties file I have a key called "flag" and I want to change it's value, I do this:
configProp.setProperty("flag", "j");
configProp.save();
System.out.println(configProp.getProperty("flag"));
"j" is rightly printed on the console. However when I open the properties file, "j" isn't there! Even if I closed down my netbeans, opened it again and ran just System.out.println(configProp.getProperty("flag"));
I still get "j", but "j" isn't anywhere in the properties file.
So where is it? Where does "j" get stored??? Is Apache Commons really that good, or am I wasting my time with it?