循环递归创建文件夹

package xx.util;

import java.io.File;

import xx.DateUtil;

public class FolderUtil {
	
	private static String[] chars={"0","1","2","3","4","5","6","7","8","9","a","b","c",
									"d","e","f","g","h","i","j","k","l","m","n","o"
									,"p","q","r","s","t","u","v","w","x","y","z"};
	private static String abPath="F:/project";//父文件夹
	private static int totalFloor=3;//总层数
	public static void main(String[] args) {
		System.out.println("******************开始 "+DateUtil.currentTime()+" 线程:"+Thread.currentThread().getName()+"**********************");
		File abFolder = new File(abPath);
		if(!abFolder.exists()){
			abFolder.mkdirs();
		}
		//我的电脑一共4个8线程,因此分出8份
		new Thread(new Runnable(){
			public void run(){
				deleteFirst(0,4);
			}
		}).start();
		new Thread(new Runnable(){	public void run(){	deleteFirst(5,9);	}	}).start();
		new Thread(new Runnable(){	public void run(){	deleteFirst(10,14);	}	}).start();
		new Thread(new Runnable(){	public void run(){	deleteFirst(15,19);	}	}).start();
		new Thread(new Runnable(){	public void run(){	deleteFirst(20,23);	}	}).start();
		new Thread(new Runnable(){	public void run(){	deleteFirst(24,27);	}	}).start();
		new Thread(new Runnable(){	public void run(){	deleteFirst(28,31);	}	}).start();
		new Thread(new Runnable(){	public void run(){	deleteFirst(32,35);	}	}).start();
	}
	/**
	 * 多线程时将36文件夹平均分开
	 * @param starChar 开始字符
	 * @param endChar	结束字符
	 */
	public static void createFirst(int starChar,int endChar){
		try {
			for (int i = starChar; i <=endChar; i++) {
				String thisPath=abPath+"/"+chars[i];//第一层
				new File(thisPath).mkdir();//这里为了提高速度没有使用mkdirs,必须人为保证父文件夹都存在
				createOther(thisPath,2);
				System.out.println(chars[i]+"创建完成 "+DateUtil.currentTime()+" 线程:"+Thread.currentThread().getName());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 循环、递归创建文件夹
	 * @param path 父路径
	 * @param charNum 当前需要创建的文件夹名
	 * @param floor	当前文件夹层数
	 */
	public static void createOther(String path,int floor) throws Exception{
		for (int i = 0; i < chars.length; i++) {
			String thisPath=path+"/"+chars[i];
			File file = new File(thisPath);
			if(!file.exists()){
				new File(thisPath).mkdir();
			}
			if(floor<totalFloor){
				createOther(thisPath,floor+1);
			}
		}
	}
	
	/**
	 * 删除文件
	 * 多线程时将36文件夹平均分开
	 * @param starChar 开始字符
	 * @param endChar	结束字符
	 */
	public static void deleteFirst(int starChar,int endChar){
		try {
			for (int i = starChar; i <=endChar; i++) {
				String thisPath=abPath+"/"+chars[i];//第一层
				File file = new File(thisPath);
				deleteOther(file);
//				file.delete();
				System.out.println(chars[i]+"删除完成 "+DateUtil.currentTime()+" 线程:"+Thread.currentThread().getName());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public static void deleteOther(File directory){
		if(!directory.exists()){
			return ;
		}
		if(!directory.isDirectory()){
			directory.delete();
		}
		File[] listFiles = directory .listFiles();//目录内文件列表
		if(listFiles.length==0){
			directory.delete();
			return;
		}
		for (int i = 0; i < listFiles.length; i++) {
			deleteOther(listFiles[i]);
		}
	}
	
}

发布了3 篇原创文章 · 获赞 1 · 访问量 1052
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览