IO流之文件夹的拷贝(递归)

文件夹的拷贝

这里用到了递归运算,即在方法里调用本方法。
拷贝文件就是我们平时用到的文件拷贝,拷贝文件夹就是递归进入文件夹内,在判断当前是文件夹还是文件,接着进行下一步。。。。



import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class DirCopy {
	/**
	 * @author 会会会会
	 * @param args
	 * @exception 文件夹的拷贝
	 *
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		copyDir("IOTest01", "IOdest");
	}

	//拷贝文件夹
	public static void copyDir(String src, String dest) {
		File filesrc = new File(src);
		File filedest = new File(dest);

		File[] listFiles = filesrc.listFiles();
		
		if (!filedest.exists()) {
			filedest.mkdir();
		}
		
		for (File file : listFiles) {

			if (file.isFile()) {
				copyFile(file.getPath(), dest + "\\" + file.getName());
			} else if (file.isDirectory()) {
				filedest.mkdir();
				copyDir(file.getPath(), dest + "\\" + file.getName());
			}
		}

	}

	// 拷贝文件
	public static void copyFile(String src, String dest) {
		File fsrc = new File(src);// 源头
		File fdest = new File(dest);// 目的地

		OutputStream os = null;
		InputStream is = null;

		try {
			is = new FileInputStream(fsrc);// 将源头输入到内存
			os = new FileOutputStream(fdest);// 准备将内存资源输出

			byte[] flush = new byte[1024];// 设置缓冲流
			int len = -1;// 定义字节节点
			while ((len = is.read(flush)) != -1) {// 判断结束
				os.write(flush, 0, len);// 字节写入输入
			}

			os.flush();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {

			try {
				if (null != os) {

					os.close();
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			try {
				if (null != is) {

					is.close();
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}
	}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
把一个文件夹复制到另一个文件夹可以使用以下代码: ```java import java.io.*; public class CopyDirectory { public static void main(String[] args) { String source = "/path/to/source/folder"; String destination = "/path/to/destination/folder"; copyDirectory(source, destination); } public static void copyDirectory(String source, String destination) { File sourceDir = new File(source); File destDir = new File(destination); if (!destDir.exists()) { destDir.mkdir(); } File[] files = sourceDir.listFiles(); for (File file : files) { String name = file.getName(); if (file.isDirectory()) { copyDirectory(source + "/" + name, destination + "/" + name); } else { copyFile(source + "/" + name, destination + "/" + name); } } } public static void copyFile(String source, String destination) { try { InputStream input = new FileInputStream(source); OutputStream output = new FileOutputStream(destination); byte[] buffer = new byte[1024]; int length; while ((length = input.read(buffer)) > 0) { output.write(buffer, 0, length); } input.close(); output.close(); } catch (IOException e) { e.printStackTrace(); } } } ``` 在上面的代码中,我们首先定义了源文件夹和目标文件夹的路径,然后调用`copyDirectory`方法来递归地复制文件夹。这个方法使用`listFiles`方法来获取源文件夹中的所有文件文件夹,然后根据文件类型来调用`copyFile`或`copyDirectory`方法进行复制。`copyFile`方法使用输入和输出流来复制文件。我们使用一个缓冲区来读取和写入文件,这样可以提高复制速度。最后,我们关闭输入和输出流,以确保所有资源都被释放。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值