Home > Java > javaTutorial > body text

Java implementation of creating a directory if it does not exist

王林
Release: 2020-05-20 16:22:49
Original
3197 people have browsed it

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()
Copy after login

Test whether the file or directory represented by this abstract path name exists.

public boolean mkdir()
Copy after login

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("//目录存在");  
}
Copy after login

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!

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!