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()
Current Working Directory Example
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
Free Spring Boot Tutorial | Full In-depth Course | Learn Spring Boot in 10 Hours
Watch this course on YouTube at Spring Boot Tutorial | Fee 10 Hours Full Course
Comments
Post a Comment