Java Get Users Home Directory


Best and safe way to get users Home directory in Java. This can be done using getUserDirectoryPath method of FileUtils class of Apache Commons IO Java library. Commons IO is well tested library. The Commons IO library contains utility classes, stream implementations, file filters, file comparators, endian transformation classes, and much more.

Methods used
//Returns the path to the user\'s home directory.
public static String getUserDirectoryPath()

//Returns a File representing the user\'s home directory.
public static File getUserDirectory()

Platform information

jdk1.8.0_60, Java 8, Eclipse

Library Information

Used library Name:

Commons IO

Maven Dependency Entry
<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.4</version>
</dependency>

Gradle Dependency Entry
compile \'commons-io:commons-io:2.4\'
Jar Download Location

Download Jar

Sample code to get users home directory

package org.code4copy;

import org.apache.commons.io.FileUtils;

public class UsersHomeDirectory {
    public static void main(String[] args) {
        System.out.println("System\'s Temp Location: " + 
                FileUtils.getUserDirectoryPath());
        System.out.println("System\'s Temp Location: " + 
                FileUtils.getUserDirectory().getAbsolutePath());
    }
}

Output of program

System\'s Temp Location: C:Usersbimbi
System\'s Temp Location: C:Usersbimbi

Download code from here


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.