zoukankan      html  css  js  c++  java
  • Java 判断多级路径是否存在,不存在就创建

    第一种方案:

    /**
    	 * 是否创建目录
    	 * @param path
    	 * @return
    	 */
    	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){
    			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;
    		}
    	}
    	

    第二种方案:

    /**
    	 * 判断文件夹是否存在
    	 * @param path 文件夹路径
    	 * true 文件不存在,false 文件存在不做任何操作
    	 */
    	public static boolean isExist(String filePath) {
    		String paths[] = filePath.split("\\");
    		String dir = paths[0];
    		for (int i = 0; i < paths.length - 2; i++) {
    			try {
    				dir = dir + "/" + paths[i + 1];
    				File dirFile = new File(dir);
    				if (!dirFile.exists()) {
    					dirFile.mkdir();
    					System.out.println("创建目录为:" + dir);
    				}
    			} catch (Exception err) {
    				System.err.println("ELS - Chart : 文件夹创建发生异常");
    			}
    		}
    		File fp = new File(filePath);
    		if(!fp.exists()){
    			return true; // 文件不存在,执行下载功能
    		}else{
    			return false; // 文件存在不做处理
    		}
    	}


    其实两种用到方法一样,只是逻辑不一样。

  • 相关阅读:
    markdown语法
    GIT基本操作
    函数rest参数和扩展
    axios基础介绍
    Vue-Resource的使用
    Vue-router的介绍
    Vue2.0+组件库总结
    Vue 项目de一些准备工作
    VUE.js入门学习(5)- 插槽和作用域插槽
    VUE.js入门学习(4)-动画特效
  • 原文地址:https://www.cnblogs.com/riasky/p/3473570.html
Copyright © 2011-2022 走看看