Servlet:
May be it is due to security policy plugged into your servlet engine. But then
you
should get a SecurityException. You can also try ServletContext.getResource,
ServletContext.getResourceAsStream
Java:
I don't get the idea behind the while-loop: As far as i understood it,
it's simply there to substitue '/' with '\', isn't it?
The File class has some fine methods which support you in filesystem
'localisation'. See J2SE doc for File.getAbsolutePath(),
File.getCanonicalPath()
You must be carefull with the later in case of symbolic links on Unix
systems. I recall that we had some problems with it in a past project.
But I think you don't need to bother anyways:
String filepath =
getServletContext().getRealPath("/File/AdminFile/adminInfo.txt");
InputStream is = new FileInputStream(filepath);
should work.
Ok for a disclaimer: I have not tried any of my hints i gave to you, so don't
'call me names' if none of them work :-)
Peter
Mamadou Bobo Sylla wrote:
> I am trying to read a file that I have stored in
> /webapps/myapp/File/AdminFile
> by using the following code:
>
> String filepath = getServletContext().getRealPath("/File/AdminFile");
> //this is to insert the prefix "\\"
> while ( st.hasMoreTokens() ) {
> String pathElement = st.nextToken();
> path.append(pathElement);
> path.append(fileSeparator);
> path.append(fileSeparator);
> }
> filepath=path.toString();
> filepath=filepath.substring(0,filepath.length()-2); //this is to remove
> the extra double slash
>
> File fileToread = new File (filepath,"adminInfo.txt");
>
> LineNumberReader br= new LineNumberReader(new FileReader(fileToread));
> while ((thisLine = br.readLine()) !=null)
> {....)
>
> I am receiving error that the system cannot find the specified file.
>
> If I copy and paste the value of the string "filepath" to the URL browser
> and press enter, I am able to open the file.
>
> I wonder where the problem lays.
>
> My concern here is that my application should be portable.
>
> Any help will be very much appreciated.
> Bobo,
>
> ___________________________________________________________________________
> 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
--
*** Bitte beachten Sie unsere neuen Mail- und Internet-Adressen ***
*** Please notice the new mail- and internet-address ***
--
___________________________________________________________________________
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