Java 确定文件夹是否存在?
我如何确定文件或目录是否已在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
谢谢。您可以调用来确定它是否存在,但如果不存在,您也可以调用来自动创建整个路径。您可以调用来确定它是否存在,但如果不存在,您也可以调用来自动创建整个路径。我通常使用这种技术:
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