Don't mix Properties.put and getProperty
Today, I made a mistake, the code is like below: I put a key/value pair, but the getProperty return null, so the code throws NPE which I didn't expect.
Today, I made a mistake, the code is like below: I put a key/value pair, but the getProperty return null, so the code throws NPE which I didn't expect.
// Don't mix put and getProperty properties.put("aint", 123); // Throws NumberFormatException, Throws NumberFormatException, as properties.getProperty("aint") return NULL. aint = Integer.parseInt(properties.getProperty("aint"));Why getProperty returns null?
The reason is that Properties extends Hashtable, put its value into a Hashtable. its getProperty method will only return String value, it first use call super.get(key) to search the Hashtable, if the key doesn't exist, or its value is not a String, it will try to get from the string value from defaults.
public String getProperty(String key) { Object oval = super.get(key); String sval = (oval instanceof String) ? (String)oval : null; return ((sval == null) && (defaults != null)) ? defaults.getProperty(key) : sval; }As we usually will read Properties from a configuration file, and later save it to the file, so it's better we always use getProperty and setProperty, but use put, and get methods.
defaults in Properties
defaults is also a Properties. When construct a new properties:newProp, we can pass a Properties:oldProp, it will save it as an instance value, and leave it here. When get value of a key, it will first search the Properties itself, it not found, it will search the defaults.
When you use remove to remove a key, it will only remove from the Properties's Hashtable, not from defaults. Later get(key) will return null as it only searches the Properties's Hashtable, but getProperty will return the value in the defaults.
If you don't like this behavior, you can use properties.putAll(aProperties); this will copy all element into the Hashtable of this new peoperties.
Summary
Java Properties is not well designed, as it extends Hashtable directly, and expose many mehtods of Hashtable which it should not. But it's still the simple way to read and save properties from/to a file.
Just be careful when we use it, always use getProperty and setProperty, not put nor get methods.
The test code is like below:
private static void testProperties() { int aint; // SHould always use setProperty and getProperty Properties properties = new Properties(); properties.setProperty("aint", "123"); aint = Integer.parseInt(properties.getProperty("aint")); // Don't mix put and getProperty properties = new Properties(); properties.put("aint", 123); // Throws NumberFormatException, as properties.getProperty("aint") return NULL. try { aint = Integer.parseInt(properties.getProperty("aint")); } catch (NumberFormatException e) { e.printStackTrace(); } Properties oldProperties = new Properties(); oldProperties.setProperty("defaultKey", "value"); Properties newProps = new Properties(oldProperties); newProps.remove("defaultKey"); // print null System.out.println(newProps.get("defaultKey")); // print value System.out.println(newProps.getProperty("defaultKey")); }