Search results
What is the best way of reading configuration parameters from configuration file in Java?
Jul 30, 2019 · In this tutorial, we introduce another approach using the java.util.prefs.Preferences class which stores and retrieves configuration data in an OS-specific way (such as registry entries on Windows OS).
- Creating a Properties object. Create a Properties object by using empty constructor: Properties props = new Properties(); Create a Properties object by supplying a default properties list
- Loading properties file. We can load the properties file (.properties or XML) using either subclasses of java.io.Reader class or java.io.InputStream class.
- Getting properties values. The Propertiesclass has two methods for retrieving value of a property in the properties file: String getProperty(String key): returns value of the property specified by the given key.
- Setting properties values. Setting value for a specific property is pretty simple, using this sole method: Object setProperty(String key, String value)
- Overview
- Loading Properties
- Get Properties
- Set Properties
- Store
- Default Property List
- Properties and Encoding
- Conclusion
Most Java applications need to use properties at some point, generally to store simple parameters as key-value pairs outside of compiled code. As such, the language has first class support for properties with java.util.Properties,a utility class designed for handling these types of configuration files. That’s what we’ll focus on in this tutorial.
2.1. From Properties Files
Let’s start with an example for loading key-value pairs from properties files. We’ll load two files we have available on our classpath: app.properties: And catalog: Notice that although it’s recommended for the properties files to use the suffix “.properties“, it’s not necessary. We can now load them very simply into a Propertiesinstance: As long as a file’s contents meet the properties file format requirements, it can be parsed correctly by the Properties class. Here are more details for Pro...
2.2. Load From XML Files
Besides properties files, the Propertiesclass can also load XML files, which conform to the specific DTD specifications. Here’s an example for loading key-value pairs from an XML file, icons.xml: Now let’s load it:
We can use getProperty(String key) and getProperty(String key, String defaultValue)to get the value by its key. If the key-value pair exists, the two methods will both return the corresponding value. But if there’s no such key-value pair, the former will return null, and the latter will return defaultValueinstead: Note that although the Properties ...
We can use the setProperty()method to update an existing key-value pair or add a new key-value pair: Note that although the Properties class inherits the put() and putAll() methods from the Hashtable class, we wouldn’t recommend using them for the same reason as the get() method; only String values can be used in Properties. The code below won’t wo...
6.1. Store to Properties Files
The Properties class provides a store()method to output key-value pairs: The second parameter is for comments. If we don’t want to write any comments, we can simply use null for it.
6.2. Store to XML Files
TheProperties class also provides a storeToXML()method to output key-value pairs in XML format: The second parameter is the same as in the store()method.
A Properties object can contain another Propertiesobject as its default property list. The default property list will be searched if the property key isn’t found in the original one. Besides “app.properties,” we have another file, “default.properties,” on our classpath: default.properties: Example Code:
By default, properties files are expected to be ISO-8859-1 (Latin-1) encoded, so properties with characters outside of the ISO-8859-1 shouldn’t generally be used. We can work around this limitation with the help of tools, such as the JDK native2ascii tool or explicit encodings on files, if necessary. For XML files, the loadFromXML() method and the ...
In this article, we discussed basic Properties class usage. We learned how to use Properties; load and store key-value pairs in both properties and XML format; operate key-value pairs in a Properties object, such as retrieve values, update values, and get its size; and finally, how to use a default list for a Propertiesobject. The complete source c...
Dec 5, 2022 · Below is a sample Java program which demonstrate you how to retrieve/read config.properties values in Java. For update follow this tutorial. Another must read: Read config.properties value using Spring “singleton” Scope in your Java Enterprise Application.
Jul 5, 2021 · Also, we use Java Config to create some initial data for the API and learned the basic HTTP methods (GET, POST, PUT, DELETE). Our API was built using the MVC standard and was divided into layers using Spring JPA (model) and Spring Web (controller) to implement the REST controllers.
People also ask
Is conf file different from txt file for Java?
Can you give an example of a configuration file in Java?
How to read and write configuration data in Java?
How to read CONFIG VALUE in Java?
How do I create a config file in Java?
How to retrieve configuration data from a preference node in Java?
Dec 9, 2020 · To do this, we have to use SnakeYAML library and it's example down below. Path configPath = Paths.get("./config.yml"); Constructor constructor = new Constructor(Config.class); Yaml yaml = new Yaml(constructor); yaml.load(new FileInputStream(configPath.toFile()));