Java 检测文件是否存在

用 File 类的 file.exists() 方法来检测文件是否存在

完整代码

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

结果输出(如果你的 C 盘中存在文件 test.txt)

true

在这里插入图片描述

发布了827 篇原创文章 · 获赞 112 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/weixin_45743799/article/details/105179345
今日推荐