如何检查路径是否存在或不在Java? [英] How to Check Path is existing or not in java?

查看:101
本文介绍了如何检查路径是否存在或不在Java?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个java程序,它将路径作为参数。在进行其他验证之前,我想检查给定的路径是否存在。例如:如果我给路径D:\ Log \Sample不存在,它必须抛出filenotfound异常。如何才能做到这一点?

解决方案

  if(!new File(D:\\ ())
{
throw new FileNotFoundException(Yikes!);
}

File.exists() ,也是 文件.isDirectory() File.isFile()


I have a java program which take path as argument. I want to check whether given path is existing or not before doing other validation. Eg: If i give a path D:\Log\Sample which is not not exist, it has to throw filenotfound exception. How can i do that?

解决方案

if (!new File("D:\\Log\\Sample").exists())
{
   throw new FileNotFoundException("Yikes!");
}

Besides File.exists(), there are also File.isDirectory() and File.isFile().

这篇关于如何检查路径是否存在或不在Java?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆