Forums Register Login

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

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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():
That feels good. Thanks. Here's a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 2336 times.
Similar Threads
Problem compiling servlet [RESOLVED]
client or consumer register listner?
an Exception report
GET method working but POST method not working
jms queue fetching of message as and when it is posted in asynchronous communication
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 11:21:42.