Home > Java > JavaBase > body text

Determine whether a directory exists in java and create it if it does not exist

王林
Release: 2019-11-25 11:28:52
Original
4689 people have browsed it

Determine whether a directory exists in java and create it if it does not exist

Knowledge supplement:

endsWith() method is used to test whether the string ends with the specified suffix.

More learning video recommendations: Introduction to java language

Examples:

 //String dirName = "D:/work/temp/temp0/temp1"; 
    public static boolean createDir(String destDirName) {  
        File dir = new File(destDirName);  
        if (dir.exists()) {  
            System.out.println("创建目录" + destDirName + "失败,目标目录已经存在");  
            return false;  
        }  
        if (!destDirName.endsWith(File.separator)) {  
            destDirName = destDirName + File.separator;  
        }  
        //创建目录  
        if (dir.mkdirs()) {  
            System.out.println("创建目录" + destDirName + "成功!");  
            return true;  
        } else {  
            System.out.println("创建目录" + destDirName + "失败!");  
            return false;  
        }  
    }
Copy after login

Related article recommendations: Introduction to java programming

The above is the detailed content of Determine whether a directory exists in java and create it if it does not exist. For more information, please follow other related articles on the PHP Chinese website!

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!