Java io copy 复制文件的几种场景

1 篇文章 0 订阅
1 篇文章 0 订阅


1、把某一磁盘目录下的所有文件(包含磁盘的深层目录) 复制到指定的一个目录的下

例如  e:/ 下所有递归到的pdf文件,复制到 d:/temp目录下



package daoImpl;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Scanner;
/**
 * @ClassName:     CopyFolder.java
 * @Description:   TODO(把某一磁盘目录递归到的指定文件类型,复制到某一目录下) 
 * 
 * @author         Administrator
 * @E-mail         809044093@qq.com 
 * @version        V1.0  
 * @Date           2014-3-19 上午09:11:46 
 */
public class CopyFolder {

	 //允许复制的文件类型
	public static String[] filterFile = { ".java", ".xml", ".xdl",
			".properties", ".sql", ".jupiter", ".wsdl" , ".pdf", ".html" };
	private long total = 0l;

	public static void main(String[] args) throws Exception {

		Scanner scanner = new Scanner(System.in);
		String answer = null;
			//源路径
			File src = new File("e:/");
			//目标路径
			File des = new File("D:/temp");
			new CopyFolder().copyFolder(src, des, filterFile);

	}

	/**
	 * 
	 * @Title: copyFolder
	 * @Description: TODO(复制文件)
	 * @param: @param srcFolder(源路径)
	 * @param: @param destFolder(目标路径)
	 * @param: @param filterFile(允许复制的文件类型)
	 * @param: @throws Exception   
	 * @return: void   
	 * @throws
	 */
	public void copyFolder(File srcFolder, File destFolder, String[] filterFile)
			throws Exception {
		File[] files = srcFolder.listFiles();
		for (File file : files) {
			if (file.isFile()) {
				String pathname = destFolder + File.separator + file.getName();
				for (String suff : filterFile) {
					if (pathname.endsWith(suff)) {
						File dest = new File(pathname);
						File destPar = dest.getParentFile();
						destPar.mkdirs();
						if (!dest.exists()) {
							dest.createNewFile();
						}
						copyFile(file, dest);
					}
				}
			} else {
				copyFolder(file, destFolder, filterFile);
			}
		}
	}

	/**
	 * 
	 * @Title: copyFile
	 * @Description: TODO(这里用一句话描述这个方法的作用)
	 * @param: @param src
	 * @param: @param dest
	 * @param: @throws Exception   
	 * @return: void   
	 * @throws
	 */
	private void copyFile(File src, File dest) throws Exception {
		FileInputStream input = null;
		FileOutputStream outstrem = null;
		try {
			 input = new FileInputStream(src);
			 outstrem = new FileOutputStream(dest);
			 outstrem.getChannel().transferFrom(input.getChannel(), 0,input.available());
			total++;
			String temp =String.format("\ncopy:%s size:%s to: %s complate: %s", src,src.length(),dest,total); 
			System.out.print(temp);
		} catch (Exception e) {
			throw e;
		} finally {
			outstrem.flush();
			outstrem.close();
			input.close();
		}
	}
}


2、递归复制文件到另外一个目录下


/**
	 * 复制一个目录及其子目录、文件到另外一个目录
	 * @param src
	 * @param dest
	 * @throws IOException
	 */
	
	private void copyFolder(File src, File dest) throws IOException {
		if (src.isDirectory()) {
			if (!dest.exists()) {
				dest.mkdir();
			}
			String files[] = src.list();
			for (String file : files) {
				File srcFile = new File(src, file);
				File destFile = new File(dest, file);
				// 递归复制
				copyFolder(srcFile, destFile);
			}
		} else {
			InputStream in = new FileInputStream(src);
			OutputStream out = new FileOutputStream(dest);

			byte[] buffer = new byte[1024];

			int length;
			
			while ((length = in.read(buffer)) > 0) {
				out.write(buffer, 0, length);
			}
			in.close();
			out.close();
		}
	}
	

方法二

        
	public void test(){
		File file = new File("E:/findbugs-2.0.2");
		File tofile = new File("c:/test");
		try {
			FileUtils.copyDirectory(file, tofile);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}















  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

prefectjava

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值