What is FileUtils.getUserDirectoryPath() in Java?

The getUserDirectoryPath() method

getUserDirectoryPath() is a staticthe methods in Java that can be called without creating an object of the class. method of the FileUtils that is used to return the path to the user’s home directory. The path to the user’s home directory is stored in the system property variable user.home.

How to import FileUtils

The definition of FileUtils can be found in the Apache Commons IO package, which we can add to the Maven project by adding the following dependency to the pom.xml file:


<dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.11.0</version>
</dependency>

For other versions of the commons-io package, refer to the Maven Repository.

You can import the FileUtils class as follows:


import org.apache.commons.io.FileUtils;

Syntax


public static String getUserDirectoryPath()

Parameters

The method has no parameters.

Return value

This method returns the path to the user’s home directory.

Code

import org.apache.commons.io.FileUtils;
public class Main{
public static void main(String[] args){
System.out.println("The path to the user's home directory is " + FileUtils.getUserDirectoryPath());
}
}

In the code above, we print the user’s home directory path obtained from the method FileUtils.getUserDirectoryPath().

Output

The output of the code will be as follows:


The path to the user's home directory is /Users/educative
New on Educative
Learn to Code
Learn any Language as a beginner
Develop a human edge in an AI powered world and learn to code with AI from our beginner friendly catalog
🏆 Leaderboard
Daily Coding Challenge
Solve a new coding challenge every day and climb the leaderboard

Free Resources