• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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: 22783
131
Eclipse IDE Spring VI Editor 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic