在Java中,可以使用java.io.File类来判断文件是否存在。通过创建一个File对象,并调用其exists()方法,可以判断文件是否存在。以下是一个示例代码:

import java.io.File;

public class FileExistenceExample {
    public static void main(String[] args) {
        String filePath = "path/to/file.txt";
        File file = new File(filePath);

        if (file.exists()) {
            System.out.println("File exists.");
        } else {
            System.out.println("File does not exist.");
        }
    }
}

在上述代码中,首先创建一个File对象,传入文件路径作为参数。然后,使用exists()方法来判断文件是否存在。如果文件存在,则打印"File exists.",否则打印"File does not exist."。请替换filePath变量为你要判断的文件路径。

Java 判断文件是否存在 - 完整指南与示例代码

原文地址: https://www.cveoy.top/t/topic/pWQh 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录