• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
  • Paul Clapham
  • paul wheaton
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Piet Souris
  • Mike London

Unable to read Properties file from Java code

Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The directory structure of my application is as follows:-
My App
++++++ src

I am trying to read the file as follows:-

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()
+ FilePath);



But none yeild any sucess while reading through the Bean class. please help...
Saloon Keeper
Posts: 26546
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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().
Don't get me started about those stupid light bulbs.
    Bookmark Topic Watch Topic
  • New Topic