• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

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

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author and iconoclast
Posts: 24203
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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():
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic