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
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
Thanks a lot sir for uploading this concept....it has been asked to me......
ReplyDeleteThank you sir for this code.
ReplyDeleteDidn't know this one before. Thanks a lot sir :)
ReplyDeleteThanks sir, same question asked to me........
ReplyDeleteGood question...Thank u sir
ReplyDelete