Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Java Properties Class

Java Properties Class

Java Data StructuresJava Data Structures


Properties inherits from Hashtable and is a class for managing configuration information.

Since Properties inherits from the Hashtable class, it has all the functionality of Hashtable and also provides some special methods to read and write property files.

The Properties class is commonly used to store program configuration information, such as database connection information, log output configuration, and application settings. Using the Properties class, this information can be stored in a text file and read from the program.

The Properties class is used by many Java classes. For example, it is the return value of the System.getProperties() method when obtaining 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:

No. 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 the property list.
3 void list(PrintStream streamOut)
 Writes the property list to the specified output stream.
4 void list(PrintWriter streamOut)
Writes the property list to the specified output stream.
5 void load(InputStream streamIn) throws IOException
 Reads a property list (key and element pairs) from the 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 put method of Hashtable.
8 void store(OutputStream streamOut, String description)
 Writes the property list (key and element pairs) of this Properties table to the output stream in a format suitable for loading into a Properties table using the load(InputStream) method.

The Properties class provides various methods to read and write property files. The most commonly used methods are the load() and store() methods.

The load() method can read properties from a file, while the store() method can write properties to a file.

The following 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 the property value

           

String

username

=

prop.

getProperty

(

"username"

)

;

           

String

password

=

prop.

getProperty

(

"password"

)

;

           

// Output the property value

           

System

.

out

.

println

(

"username: "

username

)

;

           

System

.

out

.

println

(

"password: "

password

)

;

           

// Modify the property value

            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 example above, we created a Properties object, then used the load() method to read properties from the configuration file. Next, we used the getProperty() method to obtain the property value and output it to the console. Then, we used the setProperty() method to modify the property value and 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

"

)

;

//

Show all states and capitals in hashtable.

states

=

capitals

.

keySet

(

)

;

//

get set-view of keys

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

(

)

;

//

look for state not in list -- specify default

str

=

capitals

.

getProperty

(

"

Florida

"

,

"

Not Found

"

)

;

System

.

out

.

println

(

"

The capital of Florida is

"

str

"

.

"

)

;

}

}

The compilation and execution results of the above example are 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 StructuresJava Data Structures