使用 Java 在其中创建新目录和文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18628023/
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-12 09:38:56  来源:igfitidea点击:

To create a new directory and a file within it using Java

javafiledirectory

提问by WannaBeCoder

I am trying to create a new directory and a file within this directory. Can any one tell me where am I going wrong?

我正在尝试在此目录中创建一个新目录和一个文件。谁能告诉我我哪里出错了?

I am using a Windows system and I want the directory to be present in the folder my .javafile is present.

我使用的是 Windows 系统,我希望目录出现在我的.java文件所在的文件夹中。

import java.io.*;
class  PS_Task1 {
    public static void main(String[] args) {
        try {
            File file = new File("Library\test.txt");
            file.mkdir();
            file.createNewFile();
        }
        catch(Exception e) {
            System.out.println("ecception");
        }
    }
}

采纳答案by MadProgrammer

Basically, what's happening is, you are creating a directory called Library\test.txt, then trying to create a new file called the same thing, this obviously isn't going to work.

基本上,发生的事情是,您正在创建一个名为 的目录Library\test.txt,然后尝试创建一个名为相同内容的新文件,这显然是行不通的。

So, instead of...

所以,而不是...

File file = new File("Library\test.txt");
file.mkdir();
file.createNewFile();

Try...

尝试...

File file = new File("Library\test.txt");
file.getParentFile().mkdir();
file.createNewFile();

Additional

额外的

mkdirwill not actually throw any kind of exception if it fails, which is rather annoying, so instead, I would do something more like...

mkdir如果失败,实际上不会抛出任何类型的异常,这很烦人,所以相反,我会做一些更像......

File file = new File("Library\test.txt");
if (file.getParentFile().mkdir()) {
    file.createNewFile();
} else {
    throw new IOException("Failed to create directory " + file.getParent());
}

Just so I knew what the actual problem was...

只是为了让我知道实际问题是什么......

Additional

额外的

The creation of the directory (in this context) will be at the location you ran the program from...

目录的创建(在此上下文中)将位于您从中运行程序的位置...

For example, you run the program from C:\MyAwesomJavaProjects\FileTest, the Librarydirectory will be created in this directory (ie C:\MyAwesomJavaProjects\FileTest\Library). Getting it created in the same location as your .javafile is generally not a good idea, as your application may actually be bundled into a Jar later on.

例如,您从 运行程序C:\MyAwesomJavaProjects\FileTestLibrary将在此目录中创建目录(即C:\MyAwesomJavaProjects\FileTest\Library)。在与您的.java文件相同的位置创建它通常不是一个好主意,因为您的应用程序实际上可能稍后被捆绑到一个 Jar 中。

回答by lfvv

Do this in order to create a new directory inside your project, create a file and then write on it:

这样做是为了在你的项目中创建一个新目录,创建一个文件,然后在上面写:

public static void main(String[] args) {
    //System.getProperty returns absolute path
    File f = new File(System.getProperty("user.dir")+"/folder/file.txt");
    if(!f.getParentFile().exists()){
        f.getParentFile().mkdirs();
    }
    //Remove if clause if you want to overwrite file
    if(!f.exists()){
        try {
            f.createNewFile();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    try {
        //dir will change directory and specifies file name for writer
        File dir = new File(f.getParentFile(), f.getName());
        PrintWriter writer = new PrintWriter(dir);
        writer.print("writing anything...");
        writer.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } 

}