Skip to content

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()

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
Clone this wiki locally