The Artima Developer Community
Sponsored Link

Java Answers Forum
Open file in working directory

5 replies on 1 page. Most recent reply: May 17, 2007 11:10 AM by Julius Malixi

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 5 replies on 1 page
Julius Malixi

Posts: 6
Nickname: jmalixi
Registered: Dec, 2003

Open file in working directory Posted: May 12, 2007 12:28 AM
Reply to this message Reply
Advertisement
I created a JSP in a /webapps/TEST directory of my Tomcat installation and want to simply open/read a file that also exists in this same directory.

My apps works correctly if I give the filename an absolute path:

i.e.

File configFile = new File("C:\\TEST\\test.cfg");

however, I want to avoid using a hard-coded absolute path and just open the file from where my jsp also resides. I'm sure this is doable pretty easily, but can't find a straight answer.

Any help would be appreciated.


Rajeev Mutalik

Posts: 57
Nickname: rajmutalik
Registered: Sep, 2002

Re: Open file in working directory Posted: May 14, 2007 11:30 PM
Reply to this message Reply
> I created a JSP in a /webapps/TEST directory of my Tomcat
> installation and want to simply open/read a file that also
> exists in this same directory.
>
> My apps works correctly if I give the filename an absolute
> path:
>
> i.e.
>
> File configFile = new File("C:\\TEST\\test.cfg");
>
> however, I want to avoid using a hard-coded absolute path
> and just open the file from where my jsp also resides.
> I'm sure this is doable pretty easily, but can't find a
> a straight answer.
>
> Any help would be appreciated.

I am not getting exactly whats ur working dir. What u can do is,

1. Get the working directory.
2. Instead of hard-coding the entire path, use the relative path from there.

Lets say ur JSP is in c:\tomcat\webapps\test
And when u try to read the working directory, it gives c:\tomcat.
Then u can replace ur foll line:
<b>File configFile = new File("C:\\TEST\\test.cfg");</b>
with
<b>File configFile = new File("test\\test.cfg");</b>

Hope this works for u.

Regards,
Rajeev

Julius Malixi

Posts: 6
Nickname: jmalixi
Registered: Dec, 2003

Re: Open file in working directory Posted: May 16, 2007 5:27 PM
Reply to this message Reply
Thanks for your feedback. But, that's exactly what my issue is. How do I figure out what the working directory is?

The jsp is installed under:

C:\Program Files\Apache Software Foundation\Tomcat 5.5\webapps\TEST

Is there a simple method to get "C:\Program Files\Apache Software Foundation\Tomcat 5.5\webapps\TEST" so I can open the file relative to this path?

Rajeev Mutalik

Posts: 57
Nickname: rajmutalik
Registered: Sep, 2002

Re: Open file in working directory Posted: May 16, 2007 8:44 PM
Reply to this message Reply
What u can do is:
1. Create a new temporary file with File.createTempFile()
2. Check the directory in which this file is created, this directory becomes ur working directory
3. Then get the absolute path using file.getAbsolutePath()
4. This path and subsequently the relative path(s) can be used to pinpoint to the files u wish to read/write.
5. The first three steps will be written just to find out the current working directory, once that is retrieved, you can remove those lines from the code. Because u know where the current directory is.

Any more clarification, do write bback.

Regards,
Rajeev

Rajeev Mutalik

Posts: 57
Nickname: rajmutalik
Registered: Sep, 2002

Re: Open file in working directory Posted: May 16, 2007 9:56 PM
Reply to this message Reply
Go to this link, u will find how to get the current working directory:

http://www.rgagnon.com/javadetails/java-0076.html

Regards,
Rajeev

Julius Malixi

Posts: 6
Nickname: jmalixi
Registered: Dec, 2003

Re: Open file in working directory Posted: May 17, 2007 11:10 AM
Reply to this message Reply
Thanks again for your feedback. However, this still does not work.

The output created from the sample you provided is as follows:

Current dir : C:\WINNT\system32
Parent dir : C:\WINNT

My jsp is running as a web application from: C:\Program Files\Apache Software Foundation\Tomcat 5.5\webapps\

So, obviously the current directory being returned is not what I'm looking for.

Thanks!

Flat View: This topic has 5 replies on 1 page
Topic: Open file in working directory Previous Topic   Next Topic Topic: java

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use