如何在“user.home”中创建一个目录?

4

如何在用户主目录下创建一个目录?

我知道如何创建普通目录,但如何使用user.home设置路径呢?

2个回答

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

File 中没有 mkdirs - Alexander Pogrebnyak
3
请再看一遍。http://docs.oracle.com/javase/1.5.0/docs/api/java/io/File.html#mkdirs() - Synesso

1

系统属性

为了改善帖子回答!收集所有信息并将其整合在一起。

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.
}

打印所有可用属性。
for (Entry<Object, Object> e : System.getProperties().entrySet()) {
    System.out.println(String.format("%s = %s", e.getKey(), e.getValue())); 
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接