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(); } } }
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Dreamweaver CS6
Visual web development tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Chinese version
Chinese version, very easy to use

Atom editor mac version download
The most popular open source editor