Jon Zangitu
Jon Zangitu

Reputation: 967

Trying to read a txt file from a JSP page.

I'm trying to read from a .txt file some data from a .jsp page. But I get this error:

with root cause

java.io.FileNotFoundException: Predicciones\predicciones.txt (System can't find path specified)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(FileInputStream.java:138)
    at java.io.FileInputStream.<init>(FileInputStream.java:97)
    at org.apache.jsp.getDatosPredicciones_jsp._jspService(getDatosPredicciones_jsp.java:123)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:419)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:391)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:562)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:395)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:250)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:188)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
    at java.lang.Thread.run(Thread.java:722)

The code is this:

{
        int partido = Integer.parseInt(request.getParameter("partido"));
        int irabazlea;
        float probabilidad;


        FileInputStream fstream = new FileInputStream("Predicciones\\predicciones.txt");
        DataInputStream in = new DataInputStream(fstream);
        BufferedReader br = new BufferedReader(new InputStreamReader(in));
        String strLine;
        int kont=1;
        while (((strLine = br.readLine()) != null) && (kont<=partido))

Where is the problem? The "Predicciones" directory is in the same level of the jsp.

I've tried to put the "Predicciones" directory in WEB-INF, but it doesn't work.

Upvotes: 0

Views: 2504

Answers (2)

rickz
rickz

Reputation: 4474

If your files are in your web app root directory, then try using

FileInputStream fstream = new FileInputStream(application.getRealPath("/Predicciones/predicciones.txt"));

Upvotes: 2

MaVRoSCy
MaVRoSCy

Reputation: 17839

yes but the jsp is compiled and put on a different place that the actual jsp files.

To access files from within your WEB-INF folder use

/WEB-INF/foldername/resource.ext

Please not the /.

If you use /folder/me.jpg as path, then you can access this file ww w.mydomain.com/appname/folder/me.jpg except if the application is deployed as root.In that case you will access this resource ww w.mydomain.com/folder/me.jpg

For accessing resources please read http://docs.oracle.com/javase/1.4.2/docs/guide/resources/resources.html

In your case, you can access the file using an absolute path, ex c://apache//webapps/myapp/myfolder...etc or you can use relative paths where the resource is relative to the compiled .class file

Upvotes: 0

Related Questions