Wrox Home  
Search P2P Archive for: Go

  Return to Index  

servlets thread: Why I can't open my file?


Message #1 by Edward <zhangsc@n...> on Fri, 24 May 2002 13:56:33 +0800
Edvard,

If wou want to load this file from subdirectory you can use
getClass().getResourceAsStream("data/error.txt")

regards,
Viktar

----- Original Message -----
From: "Viktar Duzh" <duzhv@s...>
To: "Servlets" <servlets@p...>
Sent: Friday, May 24, 2002 11:18 AM
Subject: [servlets] Re: Why I can't open my file?


> hi Edward,
>
> I guess You should use class loader of your GetUserIdentity.class to get
> resource error.txt.
>
> I've put error.txt file to classes directory and created class
>
> ------------------
> package com.any;
>
> import java.io.*;
>
> public class GetUserIdentity {
>      public GetUserIdentity() {
>  }
>
>      public void readError() {
>      ClassLoader classLoader = getClass().getClassLoader();
>      System.out.println("class loader = " + classLoader);
>
>      BufferedReader input = null;
>      try {
>           input = new BufferedReader(
>                  new
> InputStreamReader(classLoader.getResourceAsStream("error.txt")));
>           System.out.println("input = " + input.readLine());
>      } catch (Exception e) {
>           System.out.println("error = " + e.toString());
>      }
>  }
> }
> ----------
>
> It's Ok.
>
> Viktar
>
> ----- Original Message -----
> From: "Edward" <zhangsc@n...>
> To: "Servlets" <servlets@p...>
> Sent: Friday, May 24, 2002 8:56 AM
> Subject: [servlets] Why I can't open my file?
>
>
> >   I have a question about open file at same directory.I have some
> files,like follows:
> > D:\Apache Tomcat 4.0\webapps\login\WEB-INF\classes\GetUserIdentity.class
> > D:\Apache Tomcat 4.0\webapps\login\WEB-INF\classes\error.txt
> > The role of GetUserIdentity.class is read data from the file
> "error.txt",The file "GetUserIdentity.java" like:
> > ...
> > try
> >  {
> >    input=new BufferedReader(new FileReader("error.txt"));
> >    ...
> >   }
> > catch(IOException e)
> >
>
> >     e.printStackTrace();
> >    }
> >
> > But when I start Apache Tomcat 4.0,and I visit index.html which locate
> http://localhost:8080/login/servlets/index.html
> >
> > index.html is follows:
> > <html>
> >    <head>
> >        <title>Check Identity</title>
> >        <STYLE>.red {background-color:red;}</STYLE>
> >    </head>
> >    <body>
> >      <center>
> >      <br><br>
> >       <form action="http://localhost:8080/login/servlet/GetUserIdentity"
> method="POST">
> >       <table align="center" width="%100" cellspacing="2"
cellpadding="2">
> >         <tr>
> >            <td align="right">Input ID </td>
> >            <td><input type="Text" name="ID" align="left" size=15></td>
> >         </tr>
> >         <br><br><br>
> >         <tr>
> >           <td align="right">Input Password </td>
> >           <td><input type="password" name="password" align="left"
size=15
> color="red"></td>
> >        </tr>
> >       </table>
> >       <br><br>
> >       <input type="Submit" name="okbutton"     value="      OK      " >
> >       <input type="button" name="cancelbutton" value="    Cancel    "
> onclick="window.close()">
> >       </form>
> >      </center>
> >     </body>
> > <html>
> >
> > Then I click OK button,I got errors:
> >
> > Starting service Tomcat-Standalone
> > Apache Tomcat/4.0.1
> > Starting service Tomcat-Apache
> > Apache Tomcat/4.0.1
> > java.io.FileNotFoundException: error.txt
> >         at java.io.FileInputStream.open(Native Method)
> >         at java.io.FileInputStream.<init>(Unknown Source)
> >         at java.io.FileReader.<init>(Unknown Source)
> >         at PreReadError.<init>(PreReadError.java:22)
> >         at GetUserIdentity.<init>(GetUserIdentity.java:20)
> >         at java.lang.Class.newInstance0(Native Method)
> >         at java.lang.Class.newInstance(Unknown Source)
> >         at
> org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:82
> > 0)
> >         at
> org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.jav
> > a:615)
> >         at
> org.apache.catalina.servlets.InvokerServlet.serveRequest(InvokerServl
> > et.java:396)
> >         at
> org.apache.catalina.servlets.InvokerServlet.doPost(InvokerServlet.jav
> > a:216)
> >         at javax.servlet.http.HttpServlet.service(HttpServlet.java:760)
> >         at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
> >         at
> org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(Appl
> > icationFilterChain.java:247)
> >         at
> org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationF
> > ilterChain.java:193)
> >         at
> org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperV
> > alve.java:243)
> >         at
> org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline
> > .java:566)
> >         at
> org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.jav
> > a:472)
> >         at
> org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)
> >
> >         at
> org.apache.catalina.core.StandardContextValve.invoke(StandardContextV
> > alve.java:201)
> >         at
> org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline
> > .java:566)
> >         at
> org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.jav
> > a:472)
> >         at
> org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)
> >
> >         at
> org.apache.catalina.core.StandardContext.invoke(StandardContext.java:
> > 2344)
> >         at
> org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.j
> > ava:164)
> >         at
> org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline
> > .java:566)
> >         at
> org.apache.catalina.valves.ErrorDispatcherValve.invoke(ErrorDispatche
> > rValve.java:170)
> >         at
> org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline
> > .java:564)
> >         at
> org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.j
> > ava:170)
> >         at
> org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline
> > .java:564)
> >         at
> org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:
> > 462)
> >         at
> org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline
> > .java:564)
> >         at
> org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.jav
> > a:472)
> >         at
> org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)
> >
> >         at
> org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineVal
> > ve.java:163)
> >         at
> org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline
> > .java:566)
> >         at
> org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.jav
> > a:472)
> >         at
> org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)
> >
> >         at
> org.apache.catalina.connector.http.HttpProcessor.process(HttpProcesso
> > r.java:1011)
> >         at
> org.apache.catalina.connector.http.HttpProcessor.run(HttpProcessor.ja
> > va:1106)
> >         at java.lang.Thread.run(Unknown Source)
> >
> > I have put GetUserIdentity.class and error.txt in same dictionary,but
why
> can't find it?
> > Any idea will be appreciated!
> > Edward
> >
>
>


  Return to Index