Reputation: 10175
I just read that it is possible to store key/value pairs (properties) using the Properties
class, which is a subclass of the HashTable
. However, I have also read that you can store properties directly using System.setProperty
.
So which one do I use? What are the real use-cases of each?
Much appreciated.
Upvotes: 4
Views: 2770
Reputation: 100239
If you don't want to modify many properties, it's better to use System.setProperty(key, value)
than alternatives like System.setProperties(props)
or System.getProperties().setProperty(key, value)
. The difference is how SecurityManager
handles these calls. For System.setProperty(key, value)
the SecurityManager
is asked for permission to modify a single specific key
. For System.getProperties()
call the SecurityManager
is asked for permission to read/write anything. So it's possible that the installed SecurityManager
protects only some of properties, thus setProperty(key, value)
will succeed, but getProperties().setProperty(key, value)
will fail.
If you need to read the specific property, always use System.getProperty(key)
instead of System.getProperties().getProperty(key)
. If installed SecurityManager
disables property writing, but enables property reading, then the second call will fail.
Upvotes: 0
Reputation: 9
property
is Java.lang.System
class type of static member variable, it is java.util.Properties
type. So, System.setProperty(key)
is just the property
setter method. Hashtable
is a way of store the data,you can also consider it as a database.
You should use the System.setProperty(key)
and System.getProperty(key)
to write and read the Key/Value.
import java.io.FileInputStream;
import java.util.Properties;
public class PropertiesTest {
public static void main(String[] args)
throws Exception {
// set up new properties object
// from file "myProperties.txt"
FileInputStream propFile =
new FileInputStream( "myProperties.txt");
Properties p =
new Properties(System.getProperties());
p.load(propFile);
// set the system properties
System.setProperties(p);
// display new properties
System.getProperties().list(System.out);
}
}
Upvotes: 1
Reputation: 2874
As far as I can see System.setProperty()
uses a java.util.Properties
class, so they are using the same backing class. However the properties class that System.setProperties()
uses is retrieved from the VM, have a look at the documentation for System.getProperties() for more information on the properties set.
You probably need to consider what you are trying to achieve by setting a system property, as generally they are runtime variables describing versions and paths for the system.
In all the applications I have seen the System.properties are used as a read only list, should you want to write your properties out somewhere for the use of the system you would probably be better off putting it in a database table (if you are going to be changing these options at runtime) and working with it like that. You might want to try using a properties file if it's a relatively static list of program options that you need to be able to be different on different instances of your application, so maybe for something like branding.
The main purpose of the Properties
class is to be able to serialise the list key-value list into a stream see the Properties class
Upvotes: 0