Hi,
have a look at the following.. it is a section of a simple main() type class
that gets the path of where it is stored. This will work with servlets as
well. The problem is that with win32 the path will be in the format
/c:/classes/, so you get rid of the first character, making it c:/classes/ -
which will work.
The problem is when you try to run the same code in linux. /home/rob/classes
might end up pointing to /usr/local/java/.../home/rob/classes.. which is a big
prob. But if it is only going to be run on one platform, end of problem

best regards,
Rob

Class cls = new OutputTestMain().getClass();
URL resource = cls.getResource("../test.txt");
System.out.println(resource.toExternalForm());
System.out.println(resource.toString());
System.out.println(resource.getRef());
System.out.println(resource.getFile());

String path = resource.getFile();
path = path.substring(0,path.lastIndexOf('/')+1);

PrintWriter out;
try {
  out = new PrintWriter(new BufferedWriter(new FileWriter(path+"test.txt")));
  out.println("hello");
  out.println("wonder if this will work");
} catch ( Exception e ) {
  System.out.println("error opening file for write");
}

Laurids Pedersen <[EMAIL PROTECTED]> wrote:
> Hi Tania and all:
>
> I had the same problem: ServletContext.getRealPath() returns null in some
> environments, not on my local Jrun3 but it does when run un the
serviceprovider.
> A bit of search on the web revealed, that Jserv has a "known bug" in the
> getRealPath() implementation, so it always returns null. The advice was to
> use getResource() or getResourceAsStream() instead, but as far as I can
see,
> these only work to open inputstreams, not for writing.
>
> Any other ideas, how to manage local file IO with servlets? Relative paths
are
> no good unless you can be sure what the current path is.
>
> Best wishes
> Laurids
>
>
>
> ELECTRIC EPHEMERIS
> Laurids Pedersen
>
> ___________________________________________________________________________
> 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


____________________________________________________________________
Get free email and a permanent address at http://www.netaddress.com/?N=1

___________________________________________________________________________
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

Reply via email to