Java Properties Class
[Java Data Structures](#)
* * *
The Properties class, which inherits from Hashtable, is used for managing configuration information.
Since Properties inherits from the Hashtable class, it has all the functionality of Hashtable, while also providing some special methods for reading and writing property files.
The Properties class is commonly used to store program configuration information, such as database connection details, logging output configuration, application settings, etc. Using the Properties class, this information can be stored in a text file and read within the program.
The Properties class is used by many Java classes. For example, it is the return value of the System.getProperties() method when retrieving environment variables.
Properties defines the following instance variable. This variable holds a default property list associated with a Properties object.
Properties defaults;
The Properties class defines two constructors. The first constructor has no default value.
Properties()
The second constructor uses propDefault as the default value. In both cases, the property list is empty:
Properties(Properties propDefault)
In addition to the methods defined in Hashtable, Properties also defines the following methods:
| **Number** | **Method Description** |
| --- | --- |
| 1 | **String getProperty(String key)** Searches for the property with the specified key in this property list. |
| 2 | **String getProperty(String key, String defaultProperty)** Searches for the property with the specified key in this property list. |
| 3 | **void list(PrintStream streamOut)** Prints this property list out to the specified output stream. |
| 4 | **void list(PrintWriter streamOut)** Prints this property list out to the specified output stream. |
| 5 | **void load(InputStream streamIn) throws IOException** Reads a property list (key and element pairs) from an input stream. |
| 6 | **Enumeration propertyNames( )** Reads a property list (key and element pairs) from the input character stream in a simple line-oriented format. |
| 7 | **Object setProperty(String key, String value)** Calls the Hashtable method put. |
| 8 | **void store(OutputStream streamOut, String description)** Writes this Properties table (key and element pairs) to the output stream in a format suitable for loading into a Properties table using the load(InputStream) method. |
The Properties class provides multiple methods for reading and writing property files. The most commonly used methods are load() and store().
The load() method can read properties from a file, while the store() method can write properties to a file.
Here is a simple example demonstrating how to use the Properties class to read and write property files:
## Example
import java.io.*;
import java.util.Properties;
public class PropertiesExample {
public static void main(String[] args){
Properties prop =new Properties();
try{
// Read the property file
prop.load(new FileInputStream("config.properties"));
// Get property values
String username = prop.getProperty("username");
String password = prop.getProperty("password");
// Print property values
System.out.println("username: "+ username);
System.out.println("password: "+ password);
// Modify property values
prop.setProperty("username", "newUsername");
prop.setProperty("password", "newPassword");
// Save properties to file
prop.store(new FileOutputStream("config.properties"), null);
}catch(IOException ex){
ex.printStackTrace();
}
}
}
In the above example, we created a Properties object and then used the load() method to read properties from a configuration file. Next, we used the getProperty() method to get the property values and printed them to the console. Then, we used the setProperty() method to modify the property values and used the store() method to save the modified properties back to the file.
### Example
The following program illustrates several methods supported by this data structure:
## Example
import java.util.*; public class PropDemo{public static void main(String args[]){Properties capitals = new Properties(); Set states; String str; capitals.put("Illinois", "Springfield"); capitals.put("Missouri", "Jefferson City"); capitals.put("Washington", "Olympia"); capitals.put("California", "Sacramento"); capitals.put("Indiana", "Indianapolis"); states = capitals.keySet(); Iterator itr = states.iterator(); while(itr.hasNext()){str = (String)itr.next(); System.out.println("The capital of " + str + " is " + capitals.getProperty(str) + "."); }System.out.println(); str = capitals.getProperty("Florida", "Not Found"); System.out.println("The capital of Florida is " + str + "."); }}
The result of compiling and running the above example is as follows:
The capital of Missouri is Jefferson City.The capital of Illinois is Springfield.The capital of Indiana is Indianapolis.The capital of California is Sacramento.The capital of Washington is Olympia.The capital of Florida is Not Found.
* * Java Data Structures](#)
YouTip