Get your CodeRanch badge!*
The moose likes JSF and the fly likes Unable to read Properties file from Java code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » JSF
Bookmark "Unable to read Properties file from Java code " Watch "Unable to read Properties file from Java code " New topic
Author

Unable to read Properties file from Java code

riddhi bhatt
Greenhorn

Joined: Jul 09, 2008
Posts: 12
Hi,
The directory structure of my application is as follows:-
My App
++++++ src
++++++++com
++++++++++readProp.java
++++++++resource
++++++++++message.properties

I am trying to read the file as follows:-

<code>
public Static final string FilePath="resource.message.properties"

Here the code to read the file. I tried using the following two techniques but to no use...

File accountPropertiesFile = new File(FacesContext.getCurrentInstance()
.getExternalContext().getRequestContextPath()
+ FilePath);

properties.load(externalContext.getResourceAsStream(FilePath));

</code>

But none yeild any sucess while reading through the Bean class. please help...
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15662
    
  15

When is a file not a file? When it's part of a WAR!

A WAR is a ZIP file, and while technically we call the things stored within a ZIP, "files", they aren't files in the sense that a general file system can issue open/close/read/write calls directly on them. In a WAR context, I prefer to refer to them as "resources".

The issue is further confused because some webapp servers (such as Tomcat) support an "exploded WAR" where the WAR is unzipped, and therefore the "files" in the WAR are actually present as real files in the filesystem. But it's bad practice to depend on WAR resources being physical files because the J2EE standard does not support that concept.

However, here's what you need to do:

First, don't put your properties in the WAR root. That makes them visible from web browsers, and that's a possible security concern. You can hide stuff by putting it under the WEB-INF directory.

In a general case, such as images, you could create a directory such as "WEB-INF/images" and put your image resources in that directory. Then get the HttpServletRequest from the FacesContext and use getResourceAsStream() to obtain the handle you need to read the resource contents. The Resource pathname would be "/WEB-INF/images/picture1.jpg", for example. Note that you don't need to know the URL or the context name of the webapp, as the getResource/getResourceAsStream methods assume that the root is always going to be the top of the WAR, regardless of how and where it's deployed.

In the case of properties, however, I believe you can go one better, since unless I'm mistaken there's an api method for getting property files directly from the classpath - meaning you'd put the properties in /WEB-INF/classes or one of its subdirectories. I can't remember if it's a straight shot or if you have to have a classloader as a reference point, however. If you need a classloader, usually you can just obtain the classloader from the executing code's this.getClass().


Customer surveys are for companies who didn't pay proper attention to begin with.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Unable to read Properties file from Java code
 
Similar Threads
include directive doubt
include and file path
Log4j
Is it possible to read an ArrayList and write to an XLS file using POIFSFileSystem
How to read a property file from WAR