Amundeep Singh
Amundeep Singh

Reputation: 194

How to create a directory in "user.home"?

How would you create a directory inside the user's home?

I know how to create a normal directory, but how do you set a path for it with user.home?

Upvotes: 4

Views: 3056

Answers (2)

Yash
Yash

Reputation: 9568

System Properties

To Improving the post answer! gathered all the information and put it together.

public static void main(String[] args) {
    String myDirectory = "Yash"; // user Folder Name
    String path = getUsersHomeDir() + File.separator + myDirectory ;

    if (new File(path).mkdir()) {
        System.out.println("Directory is created!");
    }else{
        System.out.println("Failed to create directory!");
    }
    getOSInfo();
}
public static void getOSInfo(){
    String os = System.getProperty("os.name");
    String osbitVersion = System.getProperty("os.arch");
    String jvmbitVersion = System.getProperty("sun.arch.data.model");

    System.out.println(os + " : "+osbitVersion+" : "+jvmbitVersion);
}
public static String getUsersHomeDir() {
    String users_home = System.getProperty("user.home");
    return users_home.replace("\\", "/"); // to support all platforms.
}

To print all available properties.

for (Entry<Object, Object> e : System.getProperties().entrySet()) {
    System.out.println(String.format("%s = %s", e.getKey(), e.getValue())); 
}

Upvotes: 1

Synesso
Synesso

Reputation: 38978

boolean success = new java.io.File(System.getProperty("user.home"), "directory_name").mkdirs();

Upvotes: 11

Related Questions