Retrieve current directory

Please let me know how I can retrieve the current directory.
I want it to work for any operating system

Thanks!
dshrenikAsked:
Who is Participating?
 
for_yanCommented:
import java.io.File;
 public class CurrentWorkingDirectory {
 public static void main (String args[]) {
 File directory = new File (".");
 try {
 System.out.println ("Current directory's canonical path: " 
  + directory.getCanonicalPath()); 
   System.out.println ("Current directory's absolute  path: " 
  + directory.getAbsolutePath());
 }catch(Exception e) {
 System.out.println("Exceptione is ="+e.getMessage());
  }
 }
}

Open in new window

0
 
dshrenikAuthor Commented:
This will work for all OSs?
0
 
for_yanCommented:
from:
http://www.roseindia.net/java/java-get-example/get-CWD.shtml

there is also some jave command there - buiut this way it is more reliabley
0
 
for_yanCommented:

checvk this, unless this is user home, no UI think this is current:
System.getProperty("user.dir")
0
 
for_yanCommented:
http://bytes.com/topic/java/answers/16165-how-get-path-current-directory

To answer your question, try grabbing the "user.dir" property -- this
is the current working directory. To retreive this property, use
something akin to:

String userDir = System.getProperty("user.dir");
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.