Java copy all the files in the folder, list CAUTION

1. The following code can be used to copy all the files in the folder, the directory. But the destination directory is a subdirectory of the source directory to die, we will die cycle call.
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class test {
	public static void main(String[] args) throws IOException {
		copyDir("E:/Z","E:/Z/Z");
		System.out.println("复制完成!");
	}
	/**
	* 复制目录
	* @param fromDir
	* @param toDir
	* @throws IOException
	*/
	public static void copyDir(String fromDir,String toDir) throws IOException{
		//创建目录的File对象
		File dirSouce = new File(fromDir);
		//判断源目录是不是一个目录
		if (!dirSouce.isDirectory()) {
			//如果不是目录那就不复制
			return;
		}
		//创建目标目录的File对象
		File destDir = new File(toDir);	
		//如果目的目录不存在
		if(!destDir.exists()){
			//创建目的目录
			destDir.mkdir();
		}
		//获取源目录下的File对象列表
		File[]files = dirSouce.listFiles();
		for (File file : files) {
			//拼接新的fromDir(fromFile)和toDir(toFile)的路径
			String strFrom = fromDir + File.separator + file.getName();
			System.out.println(strFrom);
			String strTo = toDir + File.separator + file.getName();
			System.out.println(strTo);
			//判断File对象是目录还是文件
			//判断是否是目录
			if (file.isDirectory()) {
				//递归调用复制目录的方法
				copyDir(strFrom,strTo);
			}
			//判断是否是文件
			if (file.isFile()) {
				System.out.println("正在复制文件:"+file.getName());
				//递归调用复制文件的方法
				copyFile(strFrom,strTo);
			}
		}
	}
	/**
	* 复制文件
	* @param fromFile
	* @param toFile
	* @throws IOException
	*/
	public static void copyFile(String fromFile,String toFile) throws IOException{
		//字节输入流——读取文件
		FileInputStream in = new FileInputStream(fromFile);
		//字节输出流——写入文件
		FileOutputStream out = new FileOutputStream(toFile);
		//把读取到的内容写入新文件
		//把字节数组设置大一些   1*1024*1024=1M
		byte[] bs = new byte[1*1024*1024];	
		int count = 0;
		while((count = in.read(bs))!=-1){
			out.write(bs,0,count);
		}
		//关闭流
		in.close();
		out.flush();
		out.close();
	}
}
2. If you create tens of thousands of folders, it can not be directly deleted.

Here Insert Picture Description

3. You can write code to delete in Java, as follows.
import java.io.File;
import java.io.IOException;

public class deletetest {
	public static void main(String[] args) throws IOException {
		File fromDir = new File("E:/Z");
		copyDir(fromDir);
	}
	public static void copyDir(File fromDir){
		File[] bb = fromDir.listFiles();
		for (int i = 0; i < bb.length; i++) {
			if (bb[i].isDirectory()) {
				System.out.println(bb[i]);
				copyDir(bb[i]);
				System.out.println(bb[i].toString()+"删除文件夹"+bb[i].delete());
			}
			else{
				System.out.println(bb[i].toString()+"删除文件"+bb[i].delete());
			}
		}
	}
}

Guess you like

Origin blog.csdn.net/weixin_44547599/article/details/89717032