如何检查文件夹是否存在 [英] How to check if a folder exists
本文介绍了如何检查文件夹是否存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用新的Java 7 IO功能,实际上我试图接收文件夹的所有xml文件。但是当文件夹不存在时会引发异常,如何检查该文件夹是否与新IO一起存在?
I am playing a bit with the new Java 7 IO features, actually I trying to receive all the xml files of a folder. But this throws an exception when the folder does not exist, how can I check if the folder exists with the new IO?
public UpdateHandler(String release) {
log.info("searching for configuration files in folder " + release);
Path releaseFolder = Paths.get(release);
try(DirectoryStream<Path> stream = Files.newDirectoryStream(releaseFolder, "*.xml")){
for (Path entry: stream){
log.info("working on file " + entry.getFileName());
}
}
catch (IOException e){
log.error("error while retrieving update configuration files " + e.getMessage());
}
}
推荐答案
使用 java.nio.file.Files
:
Path path = ...;
if (Files.exists(path)) {
// ...
}
您可以选择传递此方法 LinkOption
值:
You can optionally pass this method LinkOption
values:
if (Files.exists(path, LinkOption.NOFOLLOW_LINKS)) {
还有一种方法 notExists
:
if (Files.notExists(path)) {
这篇关于如何检查文件夹是否存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文