【问题标题】:How to create a directory in "user.home"?如何在“user.home”中创建目录?
【发布时间】:2012-01-04 00:37:09
【问题描述】:

您将如何在用户家中创建目录?

我知道如何创建一个普通目录,但是如何使用user.home为其设置路径?

【问题讨论】:

    标签: java file directory


    【解决方案1】:
    boolean success = new java.io.File(System.getProperty("user.home"), "directory_name").mkdirs();
    

    【讨论】:

    【解决方案2】:

    System Properties

    改善帖子的答案!收集所有信息并将其放在一起。

    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())); 
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-04-03
      • 2010-10-20
      • 1970-01-01
      • 1970-01-01
      • 2012-02-14
      • 2011-04-07
      • 2010-12-14
      相关资源
      最近更新 更多