get the path of current Java file directory

M

Matt

I need to know how to get the path of current Java file directory.

I have the Java servlet to produce text file, and it produces to C:\hello.txt,
but I want to output in the current directory as the Java File:

i.e. C:\jakarta-tomcat-4.1.30\webapps\proj1\WEB-INF\classes

try
{
PrintWriter out = new PrintWriter(new FileWriter("hello.txt"));
out.println("HELLO");
out.close();
}
catch(IOException e)
{
}

any ideas?? Thanks!!
 
S

Sudsy

Matt said:
I need to know how to get the path of current Java file directory.

I have the Java servlet to produce text file, and it produces to C:\hello.txt,
but I want to output in the current directory as the Java File:

i.e. C:\jakarta-tomcat-4.1.30\webapps\proj1\WEB-INF\classes

Since you're running a servlet, you have access to the ServletContext
via the getServletContext() method. THAT class has a method called
getRealPath(). Try the following modification to your code:
try
{
PrintWriter out = new PrintWriter(new FileWriter("hello.txt"));

PrintWriter out = new PrintWriter(new FileWriter(
getServletContext().getRealPath("/WEB-INF/classes/hello.txt")));
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top