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
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