Use classloader's getResourceAsStream(String name) method to load the class. hint: contextclass.getResourceAsStream("/file"); will do where contextclass is the servletcontext. This will pick up from your application context. app/ file index.htm html/ jsp/ WEB-INF/ class/ lib/
This should pick it up even if its from the WAR file. If exploaded then use new FileInputStream("/file") - this should do. Mahesh ----- Original Message ----- From: "Jayson Falkner" <[EMAIL PROTECTED]> To: <[EMAIL PROTECTED]> Sent: Sunday, August 04, 2002 8:42 PM Subject: Re: Reading a file from servlet. > If you know the file will always exist (not packaged in a WAR that is) > you can use ServletContext getRealPath(), e.g.: > > String file = context.getRealPath("/WEB-INF/response.txt"); > File f = new File(file); > > Would get you the correct path to the file assuming you put the file in > the /WEB-INF directory of your web application. Be aware that in some > cases you will not have a file - the entire application can be run > directly from a WAR file. For the best portability you can use > ServletContext getResourceAsStream() and read the file as a stream. > > Cheers, > > Jayson Falkner > [EMAIL PROTECTED] > > Ravi Kumar wrote: > > Hi, > > > > Try putting the file to be read in the directory that contains the > > program that is starting your webserver. If this doesn't solve the > > problem, see if the output of this statement works: > > > > System.out.println(System.getProperty("user.dir")); //this is the > > dir where the file be put .... > > > > Hoping it helps. > > > > Ravi > > > > ----- Original Message ----- > > *From:* Nagasai Ganduri <mailto:[EMAIL PROTECTED]> > > *To:* [EMAIL PROTECTED] > > <mailto:[EMAIL PROTECTED]> > > *Sent:* Saturday, August 03, 2002 9:26 PM > > *Subject:* Reading a file from servlet. > > > > Hello Everybody, > > > > I just started writing servlets and I have a question. How do you > > read a normal text file from a servlet. I know this sounds silly > > but I have been trying to do this for sometime, but I always get a > > file not found exception. I put this line of code into the > > HelloWorld Servlet > > > > BufferedReader bR = new BufferedReader(new > > FileReader("response.txt")); > > > > and it throws the FilenotFoundException. I placed the text file in > > the same directory of the class. I have checked the permissions of > > the file and all, they are ok too. > > > > Is there any special package or method that I should be using?? > > > > Thanks in advance for help. > > > > --sai > > > > ___________________________________________________________________________ > > To unsubscribe, send email to [EMAIL PROTECTED] > > <mailto:[EMAIL PROTECTED]> and include in the body > > of the message "signoff SERVLET-INTEREST". > > > > Archives: http://archives.java.sun.com/archives/servlet-interest.html > > Resources: http://java.sun.com/products/servlet/external-resources.html > > LISTSERV Help: http://www.lsoft.com/manuals/user/user.html > > > > ___________________________________________________________________________ > To unsubscribe, send email to [EMAIL PROTECTED] and include in the body > of the message "signoff SERVLET-INTEREST". > > Archives: http://archives.java.sun.com/archives/servlet-interest.html > Resources: http://java.sun.com/products/servlet/external-resources.html > LISTSERV Help: http://www.lsoft.com/manuals/user/user.html > > ___________________________________________________________________________ To unsubscribe, send email to [EMAIL PROTECTED] and include in the body of the message "signoff SERVLET-INTEREST". Archives: http://archives.java.sun.com/archives/servlet-interest.html Resources: http://java.sun.com/products/servlet/external-resources.html LISTSERV Help: http://www.lsoft.com/manuals/user/user.html