Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Issue with property loading

 
Geetha Gubendran
Ranch Hand
Posts: 75
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Anant Jagania
Ranch Hand
Posts: 49
  • 0
  • Mark post as helpful
  • send pies
  • 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.



 
Rob Spoor
Sheriff
Pie
Posts: 20396
47
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • 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:
 
Ulf Dittmer
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic