[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Retrieve current directory

Posted on 2011-10-14
5
Medium Priority
?
188 Views
Last Modified: 2012-05-12
Please let me know how I can retrieve the current directory.
I want it to work for any operating system

Thanks!
0
Comment
Question by:dshrenik
  • 4
5 Comments
 
LVL 47

Accepted Solution

by:
for_yan earned 2000 total points
ID: 36971686
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
 

Author Comment

by:dshrenik
ID: 36971688
This will work for all OSs?
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36971690
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
 
LVL 47

Expert Comment

by:for_yan
ID: 36971698

checvk this, unless this is user home, no UI think this is current:
System.getProperty("user.dir")
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36971707
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

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month18 days, 3 hours left to enroll

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question