您的位置 首页 技术

java判断是否存在文件的方法介绍(附代码)

使用 File 类的 file.exists() 方法来检测文件是否存在:(推荐:java视频教程) import java.io.File; public class Main …

使用 File 类的 file.exists() 方法来检测文件是否存在:(推荐:java视频教程)

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判断是否存在文件的方法介绍(附代码)的详细内容,更多请关注24课堂在线网其它相关文章!

本文来自网络,不代表24小时课堂在线立场,转载请注明出处:https://www.24ketang.cn/12530.html

为您推荐

返回顶部