Loading properties from packages and subpackages

R

R

Hi All,

I have a question. I have one global properties file the {root}/res
level directory (root = default package level directory).

I have also classes like:
- mypackages.text.Parser (package: mypackages.text),
- mypackages.db.mysql.DBManager (package: mypackages.db.mysql),
- and programs in 'programs' package.

I want all classes independently of package (subpackage) level will use
my global.properties

when I do something like this:
InputStream is =
mt.getClass().getResourceAsStream("res/global.properties");
in i.e. programs package it will look of course for a file:
programs/res/global.properties
in mypackages.db.mysql file mypackages/db/mysql/res/global.properties

but I want to load "global" properties from the most top level
directory.

I can of course do something like ../../res/global.properties - but
it's not flexible at all
if I move my class to sup/sub package I will have to modify it all

and BTW how can I get OutputStream associated with class (something
like getResourceAsStream() )?

thanks in advance for your help
best regards
R
 
T

Thomas Weidenfeller

R said:
InputStream is =
mt.getClass().getResourceAsStream("res/global.properties");
in i.e. programs package it will look of course for a file:
programs/res/global.properties
in mypackages.db.mysql file mypackages/db/mysql/res/global.properties

but I want to load "global" properties from the most top level
directory.

I can of course do something like ../../res/global.properties - but
it's not flexible at all
if I move my class to sup/sub package I will have to modify it all

getResourceAsStream("/res/global.properties");
^

As explained in the Class API documentation.
and BTW how can I get OutputStream associated with class (something
like getResourceAsStream() )?

You can't - assuming you want to write changes back to your
/res/global.properties in some jar.

Instead, set up a per-user properties file, e.g. a hidden file in the
user's home directory and chain the properties lookup:

Properties default = new Properties();
InputStream is =
mt.getClass().getResourceAsStream("/res/global.properties")
default.load(is);

Properties userConfig = new Properties(default);
FileInputStream fis = new ...
userConfig.load(fis);

String ps = userConfig.getProperty("pKey");

//
// When user changes a property only set the property if
// it is different from the default
//
String newPs = ps + somethingElse;
if(!newPs.equals(default.getProperty("pKey")) {
userConfig.setProperty("pKey", newPs);
}

//
// Store (potentially) changed userConfig at some point in time.
//
FileOutputStream fos = new ...
user>config.store(fos, "comment");

/Thomas
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top