Home >Java >javaTutorial >Java implementation of creating a directory if it does not exist
This can be achieved using the exists() and mkdir() methods in the File class.
Important function introduction:
public boolean exists()
Test whether the file or directory represented by this abstract path name exists.
public boolean mkdir()
Create the directory specified by this abstract pathname.
(Recommended video tutorial: java video)
Specific examples are as follows:
File file =new File("e:/cc/cc.jpg"); //如果文件夹不存在则创建 if (!file .exists() && !file .isDirectory()) { System.out.println("//不存在"); file .mkdir(); } else { System.out.println("//目录存在"); }
Recommended tutorial: Getting started with java development
The above is the detailed content of Java implementation of creating a directory if it does not exist. For more information, please follow other related articles on the PHP Chinese website!