Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 确定文件夹是否存在?_Java_File_Filesystems_Io_Java Io - Fatal编程技术网

Java 确定文件夹是否存在?

Java 确定文件夹是否存在?,java,file,filesystems,io,java-io,Java,File,Filesystems,Io,Java Io,我如何确定文件或目录是否已在Java中创建 我基本上想创建一个数据目录,如果还没有的话 谢谢。您可以调用来确定它是否存在,但如果不存在,您也可以调用来自动创建整个路径。您可以调用来确定它是否存在,但如果不存在,您也可以调用来自动创建整个路径。我通常使用这种技术: File folderLocation = new File("/blah/blah/mysystem/myfolder"); if (folderLocation.exists()) { if (!f

我如何确定文件或目录是否已在Java中创建

我基本上想创建一个数据目录,如果还没有的话


谢谢。

您可以调用来确定它是否存在,但如果不存在,您也可以调用来自动创建整个路径。

您可以调用来确定它是否存在,但如果不存在,您也可以调用来自动创建整个路径。

我通常使用这种技术:

    File folderLocation = new File("/blah/blah/mysystem/myfolder");

    if (folderLocation.exists()) {
        if (!folderLocation .isDirectory()) {
            throw new IOException("File-system item with path [" + folderLocation.getAbsolutePath() + "] exists but is not a folder.");
        }                
    } else {
        if (!folderLocation.mkdirs()) {
            throw new IOException("Could not create folder with path : " + folderLocation.getAbsolutePath());
        }
    }

    // we are guaranteed that the folder exists here

我通常使用这种技巧:

    File folderLocation = new File("/blah/blah/mysystem/myfolder");

    if (folderLocation.exists()) {
        if (!folderLocation .isDirectory()) {
            throw new IOException("File-system item with path [" + folderLocation.getAbsolutePath() + "] exists but is not a folder.");
        }                
    } else {
        if (!folderLocation.mkdirs()) {
            throw new IOException("Could not create folder with path : " + folderLocation.getAbsolutePath());
        }
    }

    // we are guaranteed that the folder exists here