• Post Reply Bookmark Topic Watch Topic
  • New Topic

Reading Property Files  RSS feed

 
Ravissant Markenday
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone !!

got another query here, i have this property file, "ducatProperties.properties" with simple content like:

message=How are you RAVISSANT MARKENDAY

Now i want this properties file to be read by a servlet in the same location("classes" directory), DURING INITIALIZATION. So i write the following code:

public void init()
{

String message;


Properties props = new Properties();



try
{
props.load(new FileInputStream("ducatProperties.properties"));

message = props.getProperty("message");

System.out.println(message);

}

//catch exception in case properties file does not exist

catch(IOException e)
{
e.printStackTrace();
}

}

But at the server it simply shows "file not found"(something like that with the filenotfoundexception ).

Could someone please tell me how to go about this?

Thanks in advance,

warm regards,
Ravissant Markenday
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're assuming that because the servlet class and the properties file are in the same location, you wouldn't need to specify the full path. This is incorrect - what the default path is in a web application is not specified, and can be different for the various containers. The solution is either to specify the full path (determining this would involve the ServletContext.getRealPath method), or using the PropertyResourceBundle class.
 
Ravissant Markenday
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well thanks Ulf but my question was HOW DO I READ THE PROPERTIES FILE under the circumstances aforementioned by me, the server shows the error- "file no found" how do i fix that and make the contents of the "ducatProperties.properties" file to appear at the server??

Thanks again,

Raegards,
Ravissant Markenday
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You make the error go away by fixing your code in one of the two ways I've suggested. If you've never used PropertyResourceBundle, then it's probably easier to use a full path in your FileInputStream constructor.
 
Ravissant Markenday
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks so much Ulf !!! its working!!

regards,
Ravissant Markenday
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!