Home > Java > JavaBase > How to determine whether a multi-level path exists in java and create it if it does not exist

How to determine whether a multi-level path exists in java and create it if it does not exist

王林
Release: 2019-12-02 09:08:43
Original
2327 people have browsed it

How to determine whether a multi-level path exists in java and create it if it does not exist

方案一:

(带文件名的Path:如:D:\news\2014\12\abc.text)

例如:

public boolean isexitsPath(String path)throws InterruptedException{
      String [] paths=path.split("\\\\");
      StringBuffer fullPath=new StringBuffer();      
      for (int i = 0; i < paths.length; i++) {
                fullPath.append(paths[i]).append("\\\\");          
                File file=new File(fullPath.toString());          
                if(paths.length-1!=i){//判断path到文件名时,无须继续创建文件夹!              
                    if(!file.exists()){
                        file.mkdir();                  
                        System.out.println("创建目录为:"+fullPath.toString());                  
                        Thread.sleep(1500);              
                        }          
                }      
      }      
                        File file=new File(fullPath.toString());//目录全路径      
                        if (!file.exists()) {          
                            return true;      
                        }else{
                            return false;      
                        }  
}
Copy after login

注意:带文件名的path,需要判断是否path中已经包含文件名,若包含,则不再创建文件夹。

在线视频教程分享:java在线教程

(不带文件名的Path:如:D:\news\2014\12)

例如:

How to determine whether a multi-level path exists in java and create it if it does not exist

方案二:

(带文件名的Path:如:D:\news\2014\12\abc.text)

How to determine whether a multi-level path exists in java and create it if it does not exist
(不带文件名的Path:如:D:\news\2014\12)

How to determine whether a multi-level path exists in java and create it if it does not exist

注意:带文件名和不带文件名的处理方式的区别就在于循环的长度上。

推荐相关文章教程:java入门程序

The above is the detailed content of How to determine whether a multi-level path exists in java and create it 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