Pages

Footer Pages

Spring Boot

Java String API

Java Conversions

Kotlin Programs

Kotlin Conversions

Java Threads Tutorial

Java 8 Tutorial

Saturday, November 14, 2020

How To Get The Current Working Directory In Java?

1. Overview

In this tutorial, You'll learn how to find the current working directory in java. Finding current directory can be found in various ways with File API.

We should have the basic understanding on the following areas.



In the following sections, all example programs using System, Paths, FileSystems classes.

How To Get The Current Working Directory In Java?



2. Example 1 - Get Current Working Directory With System.getProperty user.dir


Let us look at the first approach.
public class CurrentDirectorySystemGetpropertyExample {

	public static void main(String[] args) {

		// Calling the getPropery() method with argument "user.dir"
		String currentDirectoryLocation = System.getProperty("user.dir");

		System.out.println("Current working directoruy : " + currentDirectoryLocation);

	}

}
Output:
Current working directoruy : /Users/javaprogramto/Documents/workspace/CoreJava
Java has a System class that provides the way to access the system properties using getProperty() method.

When you call getProperty() method, it expects a right property "user.dir" to get the current directory.

This is a simple way to get where you are in the file system.

3. Example 2 - Get Current Working Directory With Paths Class


Next approach is to get the directory using Paths.get() method.
import java.nio.file.Path;
import java.nio.file.Paths;

public class CurrentDirectoryPathGetExample {

	public static void main(String[] args) {

		// Current location from path.
		Path currentPath = Paths.get("");
		
		// getting the location from Path.toAbsolutePath()
		String currentLocation = currentPath.toAbsolutePath().toString();
		
		
		System.out.println("Current working directoruy : " + currentLocation);

	}

}

The above program also produces the same result as example 1.

First, use the Paths.get("") method to get the Path object and call toAbsolutePath() which returns the File object.

On top of the file object, just call the toString() method that gives the string representation of current path.

4. Example 3 - Get Current Directory With FileSystem.getDetaults()


Final approach is to get the Path object from FileSystem.getDefaults() method.
import java.nio.file.FileSystems;
import java.nio.file.Path;

public class CurrentDirectoryFileSystemsExample {

	public static void main(String[] args) {

		// Current location from FileSystems.
		Path currentPath = FileSystems.getDefault().getPath(".");
		
		// getting the location from Path.toAbsolutePath()
		String currentLocation = currentPath.toAbsolutePath().normalize().toString();
		
		
		System.out.println("Current working directoruy : " + currentLocation);

	}

}
The above program produces the same output. You can choose needed approach to you.

5. Conclusion


In this article, you've seen the 3 different ways to get the current working directory or location using System, Paths, FileSystems classes.

Another simpler way using File class.
String currentDir = new File("").getAbsolutePath();

GitHub Examples








No comments:

Post a Comment

Please do not add any spam links in the comments section.