如何使用 Java 在当前用户的主目录中创建文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21059085/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 06:16:19  来源:igfitidea点击:

how can I create a file in the current user's home directory using Java?

javadirectory

提问by ayeama

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/

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

}

采纳答案by MadProgrammer

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

首先,使用System.getProperty("user.home")获取“用户”目录...

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

Second, use File#mkdirsinstead of File#mkdirto ensure the entire path is created, as mkdirassumes that only the last element needs to be created

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

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

现在,您可以使用File#exists来检查抽象路径是否存在,以及它是否不File#mkdirs生成路径的所有部分(忽略那些存在的部分),例如...

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 Folderto the path ;)

请注意,我添加了一个名为Your Custom Folder路径的附加文件夹;)

回答by Tom

Note that you can use Commons-IO for this, too:

请注意,您也可以为此使用 Commons-IO:

File userDirectory = org.apache.commons.io.FileUtils.getUserDirectory();