get the path of current Java file directory

Discussion in 'Java' started by Matt, Jun 6, 2004.

  1. Matt

    Matt Guest

    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!!
     
    Matt, Jun 6, 2004
    #1
    1. Advertising

  2. Matt

    Sudsy Guest

    Matt wrote:
    > 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")));

    > out.println("HELLO");
    > out.close();
    > }
    > catch(IOException e)
    > {
    > }
     
    Sudsy, Jun 6, 2004
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. PHaroZ
    Replies:
    5
    Views:
    95,608
    akash02sap
    Aug 17, 2012
  2. Olav Tollefsen

    How to get directory path of current rquest?

    Olav Tollefsen, Oct 29, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    1,242
    Scott Mitchell [MVP]
    Oct 29, 2004
  3. Tomek
    Replies:
    2
    Views:
    85,035
    Sandip Chitale
    Oct 2, 2003
  4. Rodrigue Butaye

    get the path to the directory of the current user

    Rodrigue Butaye, Aug 15, 2003, in forum: Javascript
    Replies:
    0
    Views:
    133
    Rodrigue Butaye
    Aug 15, 2003
  5. Bob Walton
    Replies:
    9
    Views:
    218
    David K. Wall
    Sep 26, 2003
Loading...

Share This Page