I often had to deal with loading property files with applications
running on different os types written using different frameworks
(Spring/Stripes/Eclipse RCP/SWT/Guice). The problem I often had to
deal with is to locate the property file. Some frameworks modify the
class loader so in such cases it may happen, that you won't find the
property file. Apache Commons configurations is nice, but it's not
compatible with the automated property binding of guice. So I had a
look into the code and found a nice util
(ConfigurationUtils.fileFromURL(url);) in this library. Using this,
its easy to load a property file:
public File locateFile(String filePath) {
                URL url = ConfigurationUtils.locate(filePath);
                if (url == null)
                        throw new RuntimeException(
                                        MessageFormat.format("Can not find the 
file named {0}", filePath));
                File fileFromURL = ConfigurationUtils.fileFromURL(url);
                new PropertiesConfiguration().
                if (logger.isTraceEnabled())
                        logger.trace(MessageFormat.format("Located file {0} at 
{1}",
filePath, fileFromURL.getAbsolutePath()));
                return fileFromURL;
        }

new Properties().load(new FileInputStream(fileFromURL));

Richard

2009/4/2 Oscar Westra van Holthe - Kind <[email protected]>:
> On 01-04-2009 at 01:09, AK wrote:
>> On IRC I got the following suggestion for reading a properties file:
>>
>> URL aURL = getClass().getClassLoader().getResource("myApp.properties")
>>
>> The other suggestion I found online was:
>>
>> InputStream inStream = this.getClass().getClassLoader().getResourceAsStream
>> ("myApp.properties");
>>
>> Is there a preference or advantage of one way over the other?
>
> Assuming that you're reading a properties file (as opposed to a resource
> bundle stored in properties files), I'd use the second option.
>
> My reason: I can instantiate the Properties object, and call it's load(...)
> method with the InputStream.
>
>
> Oscar
>
> --
>   ,-_  Oscar Westra van holthe - Kind      http://www.xs4all.nl/~kindop/
>  /() )
>  (__ (  A: Because people normally read from top to bottom.
> =/  ()  Q: Why is top-posting such a bad thing?
>
> ------------------------------------------------------------------------------
> _______________________________________________
> Stripes-users mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/stripes-users
>

------------------------------------------------------------------------------
_______________________________________________
Stripes-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/stripes-users

Reply via email to