如何使用Java在当前用户的主目录中创建文件? [英] how can I create a file in the current user's home directory using Java?

查看:853
本文介绍了如何使用Java在当前用户的主目录中创建文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我只是想知道如何在当前用户的主目录下面创建一个自定义目录。我已经尝试了,它不工作...(代码如下)



我想要它去这个目录,并在文件夹中创建文件夹



c:/ users /user/ documents / SimpleHTML /

  File SimpleHTML = new File(C:/ Users /user/ Documents); {

//如果目录不存在,创建它
if(!SimpleHTML.exists()){
System.out.println(createing direcotry:+ SimpleHTML);
boolean result = SimpleHTML.mkdir();

if(result){
System.out.println(Direcotry created!);
}
}

new simplehtmlEditor(){
//调用打开编辑器
};

}


解决方案

使用 System.getProperty(user.home)获取用户目录...

  String path = System.getProperty(user.home)+ File.separator +Documents; 
文件customDir = new File(path);

其次,使用 文件#mkdirs 而不是 文件#MKDIR 确保创建整个路径,因为 mkdir 假定只需要创建最后一个元素



现在你可以使用 文件#存在 来检查抽象路径是否存在,如果它不是 文件#mkdirs 使所有路径的部分(忽略那些),例如..

  if(customDir.exists()|| customDir.mkdirs()){
//路径或者存在或创建
} else {
//由于某种原因无法创建路径
}

更新



可能需要做的各种检查的简单分解。前面的例子只关心路径是否存在或者可以创建它。这将打破这些检查,以便您可以看到发生了什么...

  String path = System.getProperty(user.home )+ File.separator +Documents; 
path + = File.separator +您的自定义文件夹
文件customDir = new File(path);

if(customDir.exists()){
System.out.println(customDir +already exists);
} else if(customDir.mkdirs()){
System.out.println(customDir +created);
} else {
System.out.println(customDir +未创建);
}

请注意,我添加了一个名为您的自定义文件夹到路径;)


Hello I was just wondering how to make a custom directory below the current user's home directory. I've tried this already and it doesn't work... (Code below)

I want it to go to this directory and create the folder in the documents folder

c:/users/"user"/documents/SimpleHTML/

File SimpleHTML = new File("C:/Users/"user"/Documents"); {

//  if the directory does not exist, create it
if (!SimpleHTML.exists()) {
    System.out.println("createing direcotry: " + SimpleHTML);
    boolean result = SimpleHTML.mkdir();

        if(result) {
            System.out.println("Direcotry created!");
        }
}

new simplehtmlEditor() {
    //Calling to Open the Editor
};

}

解决方案

First, use System.getProperty("user.home") to get the "user" directory...

String path = System.getProperty("user.home") + File.separator + "Documents";
File customDir = new File(path);

Second, use File#mkdirs instead of File#mkdir to ensure the entire path is created, as mkdir assumes that only the last element needs to be created

Now you can use File#exists to check if the abstract path exists and if it doesn't File#mkdirs to make ALL the parts of the path (ignoring those that do), for example...

if (customDir.exists() || customDir.mkdirs()) {
    // Path either exists or was created
} else {
    // The path could not be created for some reason
}

Updated

A simple break down of the various checks that might need to be made. The previous example only cares if the path exists OR it can be created. This breaks those checks down so that you can see what's happening...

String path = System.getProperty("user.home") + File.separator + "Documents";
path += File.separator + "Your Custom Folder"
File customDir = new File(path);

if (customDir.exists()) {
    System.out.println(customDir + " already exists");
} else if (customDir.mkdirs()) {
    System.out.println(customDir + " was created");
} else {
    System.out.println(customDir + " was not created");
}

Note, I've added an additional folder called Your Custom Folder to the path ;)

这篇关于如何使用Java在当前用户的主目录中创建文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆