Forums Register Login

Problem adding new key/value pairs to Properties object

+Pie Number of slices to send: Send
Hello,

I am currently working with a Java Properties object to hold and modify some config values for my app. What I am currently doing is reading in a properties file that I have and using that to create a Java Properties object. I then need to set one of the values based on the parameter that I pass to my app. This works perfectly as long as that key/value pair exists in the properties file that I read in; however, if the key doesn't exist, I am not able to add that key value pair. From the docs that I've read, Properties.setProperty() should add that key value pair if it doesn't exist, but it is not working that way. Am I missing something? Any help would be greatly appreciated. I've also posted some of my code below:

+Pie Number of slices to send: Send
And what does 'not working that way' mean? What do you see that is incorrect?
+Pie Number of slices to send: Send
 

Steve Luke wrote:And what does 'not working that way' mean? What do you see that is incorrect?



If my original properties file does not have a 'OMNI_CT_ENABLED' key, and I run my app, then there is no 'OMNI_CT_ENABLED' key created. If the key exists in the original properties file, then the value will be updated according to the 'useBackDoor' parameter passed in.
+Pie Number of slices to send: Send
 

J Solomon wrote:

Steve Luke wrote:And what does 'not working that way' mean? What do you see that is incorrect?



If my original properties file does not have a 'OMNI_CT_ENABLED' key, and I run my app, then there is no 'OMNI_CT_ENABLED' key created. If the key exists in the original properties file, then the value will be updated according to the 'useBackDoor' parameter passed in.



According to the javadocs, if the key does not exist, it will create the key and assign it to the value that you pass in, if no value is passed, the key will be assigned to NULL.
+Pie Number of slices to send: Send
 

J Solomon wrote:

Steve Luke wrote:And what does 'not working that way' mean? What do you see that is incorrect?



If my original properties file does not have a 'OMNI_CT_ENABLED' key, and I run my app, then there is no 'OMNI_CT_ENABLED' key created. If the key exists in the original properties file, then the value will be updated according to the 'useBackDoor' parameter passed in.



My question is how do you know it isn't created? Do you open the file via Windows explorer? Which file (old or new)? Is it not set the next time you run this application? etc... Please, details about how you prove to yourself it is or is not working.

J Solomon wrote:According to the javadocs, if the key does not exist, it will create the key and assign it to the value that you pass in, if no value is passed, the key will be assigned to NULL.


Are you sure? Can you point me to the documentation you are using? This is the behavior I would expect (mostly) but the API doesn't seem to be clear, doesn't say what you say, and is in direct contradiction with you in regards to NULL values:
From the Java 7 API

public Object setProperty(String key,
String value)
Calls the Hashtable method put. Provided for parallelism with the getProperty method. Enforces use of strings for property keys and values. The value returned is the result of the Hashtable call to put.
Parameters:
key - the key to be placed into this property list.
value - the value corresponding to key.
Returns:
the previous value of the specified key in this property list, or null if it did not have one.
Since:
1.2
See Also:
getProperty(java.lang.String)


And the Hashtable#put(Object,Object) method

put
public V put(K key,
V value)
Maps the specified key to the specified value in this hashtable. Neither the key nor the value can be null.
The value can be retrieved by calling the get method with a key that is equal to the original key.

Specified by:
put in interface Map<K,V>
Specified by:
put in class Dictionary<K,V>
Parameters:
key - the hashtable key
value - the value
Returns:
the previous value of the specified key in this hashtable, or null if it did not have one
Throws:
NullPointerException - if the key or value is null
See Also:
Object.equals(Object), get(Object)


Though setProperty() doesn't specifically say it doesn't accept null values, it does say it uses put() and put() says it doesn't accept null values. And nowhere does it say what happens when the key doesn't already exist (like I said, I expect it to behave like you said, but you said it is in the API and I don't see it. I checked back to Java 1.5 and saw no change in wording.)

A few more things to check: are you sure the value for useBackDoor is what you think it is (either "Yes" or "No")? Have you printed it out or used a debugger to be sure?
+Pie Number of slices to send: Send
 

Steve Luke wrote:My question is how do you know it isn't created? Do you open the file via Windows explorer? Which file (old or new)? Is it not set the next time you run this application? etc... Please, details about how you prove to yourself it is or is not working.



First scenario: I start with my original properties file, it has no 'OMNI_CT_ENABLED' key. I run my app and write a new file with updated properties, use Windows explorer to make sure the new file was created, and then use notepad to view contents of new properties file. There is no 'OMNI_CT_ENABLED' key in the file.

Next scenario: I start with my original properties file, that has 'OMNI_CT_ENABLED' key with value "FALSE". I run my app with 'no' passed as parameter, then use Windows explorer to make sure the new file was created, and then use notepad to view contents. Value of 'OMNI_CT_ENABLED' has been updated to "TRUE" in the file.


If I start with no properties file and run the following code, then I end up with a properties file that has the appropriate keys created, which leads me to believe that setProperty() will create the key if it doesn't exist.



I did misread the javadocs and posted incorrect info above and I do apologize for that.
+Pie Number of slices to send: Send
 

J Solomon wrote:First scenario: I start with my original properties file, it has no 'OMNI_CT_ENABLED' key. I run my app and write a new file with updated properties, use Windows explorer to make sure the new file was created, and then use notepad to view contents of new properties file. There is no 'OMNI_CT_ENABLED' key in the file.


Well, one thing that's worth noting is that the docs for store() say:
The output stream remains open after this method returns.
and I don't see anywhere that you close() it.

I'm not sure if it's the problem, but it doesn't seem right.

Winston
+Pie Number of slices to send: Send


So I added a print line just cause I wanted to see what keys were actually in the Properties object before I write out to file, and it doesn't look like that key is even added to the Properties object in memory. Again, this is only an issue if the OMNI_CT_ENABLED key does not exist in the original properties file. Any ideas?
+Pie Number of slices to send: Send
Have you done the println to prove what the useBackDoor variable holds?
+Pie Number of slices to send: Send
Figured this out, it was my own stupidity. Thanks again for all the help.
+Pie Number of slices to send: Send
 

J Solomon wrote:Figured this out, it was my own stupidity. Thanks again for all the help.


Can you post the fix so we can all learn from it please?
+Pie Number of slices to send: Send
 

Steve Luke wrote:

J Solomon wrote:Figured this out, it was my own stupidity. Thanks again for all the help.


Can you post the fix so we can all learn from it please?



The issue was with my input, I should have normalized it so I didn't hit issues with the "if statement".
expectation is the root of all heartache - shakespeare. tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 6519 times.
Similar Threads
java.io.FileNotFoundException: FileOutputStream error?
Really Challenging...HelpMe..java.Util - very Urgent
please help figure out why the standard deviaion is not giving the correct output
Convert to BufferedReader & StringTokenizor...
Threads: Multiple Readers and One writer
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 00:17:41.