
使用 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()测试此抽象路径名表示的文件或目录是否存在.
使用Atmosphere构建通过WebSocket、SSE、gRPC、WebTransport、MCP、A2A和AG-UI流传输的JVM代理网关,并带有治理钩子。
返回:当且仅当此抽象路径名表示的文件或目录存在时,返回true;否则返回false;
抛出:SecurityException如果存在安全管理器,且其SecurityManager.checkRead(java.lang.String)方法拒绝对文件或目录进行写访问.
更多java知识请关注java基础教程栏目。
Java免费学习笔记:立即使用
解锁 Java 大师之旅:从入门到精通的终极指南










