Home > Java > JavaBase > Java determines whether a file exists

Java determines whether a file exists

Release: 2019-11-19 14:46:18
Original
4100 people have browsed it

Java determines whether a file exists

java中可以使用file.exists()方法来检测文件是否存在,public boolean exists()测试此抽象路径名表示的文件或目录是否存在。

java判断文件是否存在的方法:

public static void main(String[] args) {
        File file = new File("G:\\Jeff.txt");
        File dir = new File("G:\\Jeff");
        // 判断文件是否存在
        if (file.exists()) {
            System.out.println("文件已存在");
        } else {
            System.out.println("文件不存在");
            try {
                file.createNewFile();
                System.out.println("创建文件");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        
    }
Copy after login

Java文件类以抽象的方式代表文件名和目录路径名。file类主要用于文件和目录的创建、文件的查找和文件的删除等。

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

The above is the detailed content of Java determines whether a file exists. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template