Home  >  Article  >  Java  >  Java implementation of creating a directory if it does not exist

Java implementation of creating a directory if it does not exist

王林
王林Original
2020-05-20 16:22:493201browse

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!

Statement:
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