简体   繁体   English

检查java中是否存在父目录

[英]Check if parent directory exists or not in java

How do I check whether a parent directory is created or not before creating file in that directory? 在该目录中创建文件之前,如何检查是否创建了父目录? I can use file.exists() but it only checks if my file obj is created and not my parent directory/parent path. 我可以使用file.exists(),但它仅检查是否创建了文件obj,而不是父目录/父路径。

Try that : 尝试:

 file.getParentFile().exists();

Will return true if the parent exists, false if not. 如果父级存在,将返回true,否则返回false。

public boolean exists();

that will Tests whether the file or directory denoted by this abstract pathname exists. 将测试此抽象路径名表示的文件或目录是否存在。 .

String path = "URL";
File f = new File(path);
f.exists();
f.getParentFile().mkdirs(); //File with Create Dir
f.createNewFile();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM