我有一个java程序,它以path作为参数.我想在进行其他验证之前检查给定路径是否存在.例如:如果我给出一个不存在的路径D:\ Log\Sample,它必须抛出filenotfound异常.我怎样才能做到这一点?
Zac*_*ena 27
if (!new File("D:\\Log\\Sample").exists())
{
throw new FileNotFoundException("Yikes!");
}
Run Code Online (Sandbox Code Playgroud)
此外File.exists(),还有File.isDirectory()和File.isFile().
Rom*_*las 12
java.io.File类可以为您处理:
File f = new File("....");
if (!f.exists()) {
// The directory does not exist.
...
} else if (!f.isDirectory()) {
// It is not a directory (i.e. it is a file).
...
}
Run Code Online (Sandbox Code Playgroud)
新文件(路径).exists()。
阅读 javadoc 非常有用,并且经常提供许多有用的示例。
| 归档时间: |
|
| 查看次数: |
54400 次 |
| 最近记录: |