-
Notifications
You must be signed in to change notification settings - Fork 8
How to get the current working directory in Java
Ramesh Fadatare edited this page Jul 21, 2018
·
1 revision
The current working directory means the root folder of your current Java project, it can be retrieved by using the following system property function.
There are two ways to get current working directory.
String workingDir = System.getProperty("user.dir");
FileSystems.getDefault().getPath("").toAbsolutePath().toString()
package com.javaguides.javaio.fileoperations.fileexamples;
import java.nio.file.FileSystems;
/**
* get current working directory.
* @author javaguides.net
*
*/
public class CurrentWorkingDirectory {
/**
* getCurrentWorkingDirectoryPath
*
* @return
*/
public static String getCurrentWorkingDirectoryPath() {
return FileSystems.getDefault().getPath("").toAbsolutePath().toString();
}
/**
* getCurrentWorkingDirectoryPath
*
* @return
*/
public static String getCurrentWorkingDirectory() {
return System.getProperty("user.dir");
}
public static void main(String[] args) {
System.out.println(getCurrentWorkingDirectory());
System.out.println(getCurrentWorkingDirectoryPath());
}
}
Output:
C:\Project_Work\workspace\java-io-guide
C:\Project_Work\workspace\java-io-guide