Getting the path of current directory is often required when you need to access some files from code generally for reading text or properties files.

This article will explain 4 different ways to get the path of current working directory in java or from java code.

Method 1: Using System property
Java has defined some properties which are used to get information about its environment. One of such property is user.dir which gives the path of current working directory.

In order to retrieve the value of java properties, getProperty() method of java.lang.System class is used. This method is static and takes a string argument, which is the name of property.

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

If you run this code from an IDE such as eclipse, then it will return the absolute path of the current project.

Method 2: Using File class class has a constructor which takes a string argument. This argument represents the path to which the file object points.

Providing an empty path means that the file object is pointed to the current folder. Invoke getAbsolutePath() on the file object to get the complete path of current directory as shown below.

// create file object for current folder
File file = new File("");
String currentPath = file.getAbsolutePath();
System.out.println("Current path is:: " + currentPath);

Method 3: Using Paths
java.nio.file.Paths class introduced in java 7 is a utility class that contains static methods related to paths. It has a static get() method which takes a string argument representing a file path and returns an object of java.nio.file.Path.

Supplying empty string to get() means the current working directory.
Path object points to a physical file and its toAbsolutePath() method returns another Path object containing the absolute path of the file.

Invoke toString() method on this Path object to get the path of current working directory.
Java program code for this method is given below.

Path currentDirectoryPath = Paths.get("").toAbsolutePath();
String currentPath = currentDirectoryPath.toString();
System.out.println("Current directory path:: " + currentPath);

Method 4: Using FileSystems class
For dealing with file system, java provides a class java.nio.file.FileSystem. Object of FileSystem can be retrieved using java.nio.file.FileSystems class using its static getDefault() method.

FileSystem has a getPath() method which takes a string argument representing the path of a file.
Supplying it an empty string means the current directory.
getPath() returns an object of Path and you can use its toAbsolutePath() method to get the full path of current directory.

FileSystem fileSystem = FileSystems.getDefault();
Path path = fileSystem.getPath("").toAbsolutePath()
String currentDirectoryPath = path.toString();

Above code can be written as one-liner

String currentDirectoryPath = FileSystems.getDefault().

Hope the article was useful, click the clap below if you liked it.


Share your thoughts !!

Close Menu

Never Miss an article !

Get the new post delivered straight into your inbox, enter your email and hit the button

You have successfully subscribed to the newsletter

There was an error while trying to send your request. Please try again.

codippa will use the information you provide on this form to be in touch with you and to provide updates and marketing.