为什么这段代码不创建文件?

Why is this code not creating files?

我正在尝试使用这些方法创建文件:

private boolean createFileMain(String path){
    File file = new File(path);
    if(file.isDirectory()){
        return this.createDirectory(file);
    } else if(file.isFile()) {
        return this.createFile(file);
    } else {
        return false;
    }
}

private boolean createFile(File file){
    if(!file.exists()){
        if(file.getParentFile().exists()){
            try{
                if(file.createNewFile()){
                    return true;
                }
            }catch(IOException e){
                return false;
            }
        } else {
            if(this.createDirectory(file)){
                this.createFile(file);
            } else {
                return false;
            }
        }
    }
    return true;
}

private boolean createDirectory(File file){
    if(!file.exists()){
        if(file.mkdirs()){
            return true;
        }
        return false;
    }
    return true;
}

文件路径:

/Users/username/Directory/Accounts/

/Users/username/Directory/Srcs/file1.txt

/Users/username/Directory/file2.txt

当我尝试 运行 时,以下方法抛出 WhosebugError

public void writeInFile(String path, List<String> content) {
    if ((new File(path)).exists()) {
        try {
            writer = new PrintWriter(path, "ASCII");
            for (String contentItem : content) {
                writer.println(contentItem);
            }
            writer.close();
        } catch (FileNotFoundException e1) {
            //DO STUFF
        } catch (UnsupportedEncodingException e) {
            //DO STUFF
        }
    } else {
        this.createFileMain(path);
        this.writeInFile(path, content);
    }

为什么没有创建文件?

您是否阅读了 isDirectory() 等方面的 JavaDocs?对于 isDirectory(),它表示:

returns true if and only if the file denoted by this abstract pathname exists and is a directory; false otherwise

因此,如果该目录不存在,您会得到 false 并且不会创建目录。然后你继续尝试写、创建、写等等,直到你得到 WhosebugError。

要修复 Whosebug,您应该检查创建文件的 return 值,例如

boolean created = this.createFileMain(path);
if( created ) {
  this.writeInFile(path, content);
}

要修复您的 file/directory 创建,您需要检查文件是否已经存在,否则就创建它(也可以选择通过 file.getParentFile().mkdirs() 创建父目录)。

问题是你应该知道你是想创建文件还是目录,因为你不能仅通过名称来判断路径是目录还是文件名(除非你发明一些标记,例如始终以分隔符结束目录路径或要求文件始终具有扩展名)。如果你想写一些内容你无论如何都需要创建一个文件,一个目录会再次破坏你的代码。

private boolean createFile(File file) {
    if(!file.exists()) {
        if(file.getParentFile().exists()) {
            // ...
        } else { // In this case, neither file nor its parent exist
            if(this.createDirectory(file)) {
                this.createFile(file); // HERE, you're calling the same method again
            } else {
                // ...;
            }
        }
    }
    return true;
}

我相信您想将标记为 HERE 的行替换为 this.createFile(file.getParentFile());。这里发生的事情是你的函数用相同的参数递归地调用自己,所以什么都没有发生,你的程序陷入循环,直到它用完堆栈内存。

您的 createFileMain 只创建已经存在的文件。

你不需要创建一个文件来写入它,你只需要你想要写入的目录。

public void writeInFile(String path, List<String> content) {
    File file = new File(path);
    File parent = file.getParentFile();
    if (parent != null && !parent.exists())
        parent.mkdirs();

    try (PrintWriter writer = new PrintWriter(path, "ASCII")) {
        for (String contentItem : content) {
            writer.println(contentItem);
        }
        writer.close();
    } catch (IOException e1) {
        //DO STUFF
    }
}