• 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 ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Issue with property loading

 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying to load properties like the below.

try {
System.out.println("I am in");
FileInputStream in = new FileInputStream("CommonUtil.properties");
GSTValues.load(in);
} catch(FileNotFoundException fnf){
} catch(IOException io) {
}



I have the file which is using the above code and the properties file in the same package. But then the properties are not loaded. can someone suggest me a solution
 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Before answering, make sure that next time you put the code in "code" tags.

You need to have the properties file in the class path. If it is not there, it will not be loaded.


FileInputStream loads the file from a location/absolute path. So you need to provide the full path to you properties file in the constructor of FileInputStream.
If you want to load properties file from classpath which is the ideal situation because you dont want to give the absolute path of the properties then you can do something like following.



 
Sheriff
Posts: 22849
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You shouldn't use FileInputStream for resources; these resources might be located inside a JAR file where FileInputStream cannot handle them.

Forget about FileInputStream and use Class.getResourceAsStream or ClassLoader.getResourceAsStream:
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


} catch(FileNotFoundException fnf){
} catch(IOException io) {
}


And, of course, never, ever, ignore I/O exceptions. You simply MUST handle them, or at least print an error message where you'll notice it.
reply
    Bookmark Topic Watch Topic
  • New Topic