循环递归创建文件夹

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]);
		}
	}
	
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值