您的位置 首页 java

java读取文件是否存在的方法

以下实例演示了使用 File 类的 file.exists() 方法来检测文件是否存在:

import java.io.File;public class Main {   public static void main(String[] args) {      File file = new File("C:/java.txt");      System.out.println(file.exists());   }}

运行结果为(如果你的 C 盘中存在文件 java.txt):

true

exists():

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

返回:当且仅当此抽象路径名表示的文件或目录存在时,返回true;否则返回false;

抛出:SecurityExcepTIon如果存在安全管理器,且其SecurityManager.checkRead(java.lang.String)方法拒绝对文件或目录进行写访问。

更多java知识请关注java基础教程栏目。

文章来源:智云一二三科技

文章标题:java读取文件是否存在的方法

文章地址:https://www.zhihuclub.com/20701.shtml

关于作者: 智云科技

热门文章

网站地图