1、判断文件夹是否存在,不存在则创建:(java相关视频教程推荐:java视频教程)
File folder = new File("d:\\test1\\test2"); if (!folder.exists() && !folder.isDirectory()) { folder.mkdirs(); System.out.println("创建文件夹"); } else { System.out.println("文件夹已存在"); }
public boolean exists()测试此抽象路径名表示的文件或目录是否存在,返回:当且仅当此抽象路径名表示的文件或目录存在时,返回true;否则返回false。
java中的isDirectory()是检查一个对象是否是文件夹。返回值是boolean类型的。如果是则返回true,否则返回false。
调用方法为:对象.isDirectory() 无需指定参数。
2、判断文件是否存在,不存在则创建
File file = new File("d:\\test.txt"); if (!file.exists()) { try { file.createNewFile(); } catch (IOException e) { e.printStackTrace(); } System.out.println("文件已创建"); } else { System.out.println("文件已存在"); }
更多java相关文章请关注java基础教程栏目。
The above is the detailed content of How to determine whether a file folder exists in java (code attached). 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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

SublimeText3 English version
Recommended: Win version, supports code prompts!

Dreamweaver CS6
Visual web development tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft