Home >Java >Javagetting Started >How to determine whether a file exists in java

How to determine whether a file exists in java

(*-*)浩
(*-*)浩Original
2019-11-19 13:13:563193browse

How to determine whether a file exists in java

可以使用Java的文件类方法exists(),它可以测试给定的路径名表示的文件或目录是否存在。如果存在的话返回“true”,否则就是返回“false”。        (推荐学习:java课程

Java文件类以抽象的方式代表文件名和目录路径名。该类主要用于文件和目录的创建、文件的查找和文件的删除等。

public boolean exists()

测试此抽象路径名表示的文件或目录是否存在。    

实例:

String filePth= "D:\\bh\\key\\rsa_public_key.pem";
File file = new File(filePth);
if(!file.exists()){
	// 文件不存在
	throw new RuntimeException("文件不存在,请联系管理员");
}

The above is the detailed content of How to determine whether a file exists in java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn