Please open the menu to show more

Friday, August 11, 2017

Simple but very important code asked in Java based companies

How to get Current Working Directory (CWD) in Java. 

In Java Current Working Directory can be retrieved using the following code

Note: Please save this class inside GetCurrentDirectory.java file inside Java Project of Eclipse

public class GetCurrentDirectory {
public static void main(String[] args) {

String dir = System.getProperty("user.dir");
System.out.println("Current directory is "+dir);

}

}

Explanation of code :
note-1: getProperty() is a static method of System class
note-2: user.dir is a pre defined property (kind of a key)
note-3: on the basis of user.dir key, the getProperty() method retuns the complete
            path of current Working directory  

5 comments:

  1. Thanks a lot sir for uploading this concept....it has been asked to me......

    ReplyDelete
  2. Thank you sir for this code.

    ReplyDelete
  3. Didn't know this one before. Thanks a lot sir :)

    ReplyDelete
  4. Thanks sir, same question asked to me........

    ReplyDelete
  5. Good question...Thank u sir

    ReplyDelete