Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / April 2006

Tip: Looking for answers? Try searching our database.

Loading properties from packages and subpackages

Thread view: 
R - 19 Apr 2006 15:09 GMT
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
Thomas Weidenfeller - 19 Apr 2006 15:27 GMT
> InputStream is =
> mt.getClass().getResourceAsStream("res/global.properties");
[quoted text clipped - 8 lines]
> 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
Signature

The comp.lang.java.gui FAQ:
ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq
http://www.uni-giessen.de/faq/archiv/computer-lang.java.gui.faq/



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.