How can I get current working directory?

A system properties named user.dir can be used if you want to find the current working directory of your Java program.

package org.kodejava.example.io;

public class CurrentDirectoryExample {
    public static void main(String[] args) {
        // System property key to get current working directory.
        String USER_DIR_KEY = "user.dir";
        String currentDir = System.getProperty(USER_DIR_KEY);

        System.out.println("Working Directory: " + currentDir);
    }
}

Result example:

Working Directory: /Users/wsaryada/Projects/kodejava

How do I get user home directory name?

package org.kodejava.example.lang;

public class UserHomeExample {
    public static void main(String[] args) {
        // This is the key that we used to obtain user home directory
        // in the operating system
        String userHome = "user.home";

        // We get the path by getting the system property with the 
        // defined key above.
        String path = System.getProperty(userHome);

        // We print your home path here
        System.out.println("Your Home Path: " + path);
    }
}

On my machine it print something like:

Your Home Path: /Users/wsaryada