CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Aug 2006
    Posts
    230

    why i cant read simple text file in jsp

    Hello all
    i have this simple function that will read me line by line from giving text file
    that is in the under webapps dir of my tomcat


    public String getConst(String category,String param){
    String filename = "\\conf.txt";

    try{
    java.io.BufferedReader in = new java.io.BufferedReader(new java.io.FileReader( filename) );
    String str;
    while ((str = in.readLine()) != null) {
    str+=str;
    }
    return str;
    }catch(java.io.IOException e){
    return "file not found";
    }
    }







    and stillim geting file not found all the time why is that?

  2. #2
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: why i cant read simple text file in jsp

    What is the actual exception error message?

    In theory, there is no difference between theory and practice, but not in practice...
    Anon.
    Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  3. #3
    Join Date
    Aug 2006
    Posts
    230

    Re: why i cant read simple text file in jsp

    java.io.IOException
    im geting

  4. #4
    Join Date
    Apr 2006
    Location
    India
    Posts
    214

    Re: why i cant read simple text file in jsp

    Quote Originally Posted by umen
    Hello all
    i have this simple function that will read me line by line from giving text file
    that is in the under webapps dir of my tomcat


    public String getConst(String category,String param){
    String filename = "\\conf.txt";

    try{
    java.io.BufferedReader in = new java.io.BufferedReader(new java.io.FileReader( filename) );
    String str;
    while ((str = in.readLine()) != null) {
    str+=str;
    }
    return str;
    }catch(java.io.IOException e){
    return "file not found";
    }
    }







    and stillim geting file not found all the time why is that?
    Your exception says it all , file is not found. Why ? wrong path,
    check tomcat docs/mail list. Try the Tomcat forum
    TC user forum
    "It is a capital mistake to theorize before one has data. Insensibly one begins to twist facts to suit theories, instead of theories to suit facts." Quote from Sherlock Holmes

  5. #5
    Join Date
    Dec 2005
    Posts
    251

    Re: why i cant read simple text file in jsp

    Your exception says it all , file is not found...
    Actually the OP is throwing away the exception and returning his own "file not found" message. The only way you can really be sure of the cause of the exception is to not throw it away an use the infomation that the exception is providing (read: stack trace).

  6. #6
    Join Date
    Apr 2006
    Location
    India
    Posts
    214

    Re: why i cant read simple text file in jsp

    Quote Originally Posted by gmrowe1075
    Actually the OP is throwing away the exception and returning his own "file not found" message. The only way you can really be sure of the cause of the exception is to not throw it away an use the infomation that the exception is providing (read: stack trace).
    Well , actually I saw what the OP is returning in that catch block, and also read the code for file read etc., Iam pretty sure it should be file not found with that path et al

    Ok , I will be more specific try reading the javax.servlet, api on how to access files inside your web application running inside a container, read about the Interface javax.servlet.ServletContext.
    "It is a capital mistake to theorize before one has data. Insensibly one begins to twist facts to suit theories, instead of theories to suit facts." Quote from Sherlock Holmes

  7. #7
    Join Date
    Aug 2006
    Posts
    230

    Re: why i cant read simple text file in jsp

    the file is for sure there ?
    but mybe i can't read the file from the jsp page levle , mybe it need to be
    servlet , i dont know..

  8. #8
    Join Date
    Apr 2003
    Location
    Los Angeles area
    Posts
    776

    Re: why i cant read simple text file in jsp

    Try placing your file in the web-inf folder.
    "The Chicken and Rice MRE is not a personal lubricant."

  9. #9
    Join Date
    Oct 2003
    Location
    .NET2.0 / VS2005 Developer
    Posts
    7,104

    Re: why i cant read simple text file in jsp

    Quote Originally Posted by umen
    the file is for sure there ?
    but mybe i can't read the file from the jsp page levle , mybe it need to be
    servlet , i dont know..
    as a start, you could paste us the contents of the exception stack trace (like was asked originally)?
    "it's a fax from your dog, Mr Dansworth. It looks like your cat" - Gary Larson...DW1: Data Walkthroughs 1.1...DW2: Data Walkthroughs 2.0...DDS: The DataSet Designer Surface...ANO: ADO.NET2 Orientation...DAN: Deeper ADO.NET...DNU...PQ

  10. #10
    Join Date
    Aug 2006
    Posts
    2

    Re: why i cant read simple text file in jsp

    Here is what you should do.

    // obtain a reference to ServletContext
    ServletContext context = pageContext.getServletContext();

    // create a file object to point to a directory residing on the server
    // here, I am assuming that you have your file in the 'web' directory
    File dir = new File( context.getRealPath( "web" ) );
    // create a file object that encapsulates the file you wish to read
    File file = new File( dir, "filename.extension" );

    // now check that the file exists
    if( file.exists() )
    {
    out.println( "<h1>File contains</h1>" );
    BufferedReader buf = new BufferedReader( new FileReader( file ) );
    String output = "";
    while( ( output = buf.readLine() ) != null )
    {
    out.println( output );
    } // ends while
    // close buffer
    buf.close();
    } // ends if statement
    else
    {
    out.println( "File does not exist." );
    } // ends else

    Please note that the servlet generated from running the JSP is ought to handle the IO exception for you, and that is why I haven't bothered including a try and catch block. However, if you wish to handle any IOExceptions within the JSP page, I don't see why you shouldn't do that.

    I hope this helps.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured