Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Unable to Read a file when the method of the class called from a servlet  RSS feed

 
Jayesh Netravali
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a class IOTest.java which makes a FileInputStream object by taking a file path. This class is in a package and the File to be read is in the same package.
The FileInputStream is used to load a Property File. This class( IOTest.java) when run standalone i.e. with main() runs fine and it reads the property file. But in the actual application I need to call a static method of this class from a servlet. When this is done I get a FileNotFoundException. The same class ( IOTest) does not find the file .
What is the problem and How can I solve this ?
The Code is as follows :-
package com.jayesh.util;
import java.io.*;
import java.util.*;
public class IOTest
{
public static void load()
{

String propertyName = "";
String propertyValue = "";
Properties property = new Properties();
Enumeration numberOfProperties;
File f = new File ( "com/jayesh/util/FILE_PATH.properties" );
try
{
FileInputStream fin = new FileInputStream( f );
property.load( fin );
numberOfProperties = property.propertyNames();
while ( numberOfProperties.hasMoreElements() )
{
//getting the property name
propertyName = (String) numberOfProperties
.nextElement();
//getting the corresponding property value
propertyValue = property.getProperty( propertyName );
System.out.println("propertyName " + propertyName );
System.out.println(" propertyValue " +propertyValue );
}
}
catch( Exception e)
{
System.out.println("Exception" + e.toString() );
}
}
}

Servlet Code
ackage com.jayesh.servlet;

import com.jayesh.util.IOTest;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class TestServlet extends HttpServlet
{
ServletContext context;
public void init( ServletConfig config ) throws ServletException
{
super.init( config );
context = config.getServletContext( );
//Load Generic Property Files
IOTest.load();
}


public void doPost( HttpServletRequest request, HttpServletResponse response ) throws ServletException,IOException
{
response.setContentType("text/html" );
PrintWriter out = response.getWriter();
String strName = request.getParameter("txtName");
out.println("YouEntered" + strName );
}
public void doGet(HttpServletRequest request, HttpServletResponse response )throws ServletException,IOException
{
doPost(request,response);
}
}
jayeshnetravali@yahoo.com
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24215
37
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This code assumes that the working directory is the same directory in
which "com", the top of the package hierarchy, is located (the hard-coded file path is relative to this directory.) In a servlet, you can't depend on where the directory is.
The right way to read a properties file in any Java code is to use Class.getResource() or Class.getResourceAsStream():
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!