Properties in Java Java.util.Properties -Example--Java Collections
Properties implements Cloneable, Serializable, Map ---Java
Collections -Properties
Java.util.Properties
Java Collections -Properties
Properties implements Cloneable, Serializable, Map
Example
import java.io.File;
import
java.io.FileInputStream;
import
java.io.FileNotFoundException;
import
java.io.IOException;
import
java.io.InputStream;
import
java.util.Enumeration;
import
java.util.Hashtable;
import
java.util.Properties;
/**
*
This
class
Demonstrates
a
way
to
use
properties.
*
In
huge
applications
projects
may
need
to
have
many
things
as
configurable
properties,
*
this
may
be
because
the
business
needs.
*
It
is
a
very
bad
habbit
to
hard
code
the
values
in
the
program.
*
It's
best
practise
to
use
properties
in
a
property
file.
*
*
Also
most
desginers
will
have
this
in
mind
for
any
application.
*
Generally
the
properties
loaded
from
the
file
are
kept
in
the
application
scope.
*
So
the
properties
will
be
loaded
once
when
the
application
triggers
a
start
point.
*
Web
environment,
best
way
to
initiliaze
properties,
is
when
*
Application
Context
is
started.
*
Write
a
class
which
implements
HttpContextListener,
call
your
property
initializer
in
context
startup.
*
*
*
Your
File
may
have
duplicate
entries,
generally
you
have
to
avoid
duplicate
entries
*
in
the
file
*
@author
Darur
*
*/
public
class
PropertiesDemo {
//
/**
*
@return
*/
public
static
Properties readPropertiesFromFile( String fileName )
{
Properties properties =
new
Properties();
InputStream inStream;
try {
inStream =
new
FileInputStream(
new
File( fileName ) );
properties.load(inStream);
} catch(
FileNotFoundException fileNotFoundException ){
System. err.println(
"FILE NOT
FOUND EXCEPTION WHILE READING THE FILE: "+
fileName +
"\n" + fileNotFoundException);
} catch(
IOException ioException ){
System. err.println(
"IOEXCETION
WHILE READING THE FILE: " +
fileName +
"\n" + ioException);
}
return
properties;
}
/**
*
add
all
the
prope
*
@param
args
*/
public
static
Hashtable buildPropertiesTable( Properties
properties ){
Hashtable hashTable =
new
Hashtable();
Enumeration enumeration =
properties.keys();
while (
enumeration.hasMoreElements() ){
String key =(String)
enumeration.nextElement();
//key is a string in
property file
String value =
properties.getProperty(key);
hashTable.put(key, value);
}
return
hashTable;
}
public
static
void
main( String [] args ){
Properties properties =
readPropertiesFromFile(
"C:\\properties.txt"
);
Hashtable hashTable =
buildPropertiesTable(properties);
HashTableDemo hashTableDemo =
new
HashTableDemo();
// Remember we wrote this class
HashTableDemo. Follow this link if you need this.
hashTableDemo.prinHashTable(
hashTable );
}
}
Output :
usertype : premium
port : 1046
driver : jdbc
connectionport : 127.0.0.1
------------------------------------------------------------------------------------
Sample Properties in
this file:
usertype = premium
driver = jdbc
connectionport = 127.0.0.1
port = 1040
port = 1041
port = 1042
port = 1043
port = 1044
port = 1045
port = 1046
Save this in file and
process in main method
Please send comments to vgdarur.javafive@blogger.com
|