package com.qzkj.IO;


/**
 * 文件复制
 */
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class CopyFile {
	
	public static void main(String[] argc) {
		CopyFile copy = new CopyFile();
		         // 方法参数为(起始路径,目标路径,文件后缀,文件名关键字, “*”表示所有文件)
		copy.copyFils("f:\\java", "e:\\test", "txt", "*");

	}
	// 统计被扫描的文件个数
	public int counter ;

	/*
	 * 从起始地址复制指定后缀名的文件到目标地址
	 */
	public void copyFils(String start, String aim, String lastname,
			String keyword) {
		// 复制的文件计数
		int count = 0;


		// 调用outputFiles方法,得到指定后缀名的文件路径集合
		List<String> file = outputFiles(start, lastname);

		for (int i = 0; i < file.size(); i++) {
			// 获取文件路径
			String path = file.get(i);
			// 剪裁文件后缀,用来判断文件类型,是属于txt,java
			String ename = path.substring(path.lastIndexOf("."));
			//获取文件名
			String newName = path.substring(path.lastIndexOf("\\") + 1,
					path.lastIndexOf("."));//???????
			// 所有文件都放到目标文件夹里
			String aimPath = aim + "\\" + newName + "." + ename;//??????
			// 判断文件名是否含有关键字
			if (keyword.equals("*") || newName.indexOf(keyword) != (-1)) {
				// 判断目标目录里是否存在同名的文件
				if (new File(aimPath).exists()) {
					// 存在同名则重命名
					newName = newName + "(" + i + ")";
					System.out.println("!!!存在重名文件,已重命名成:" + newName);
				}
				// 新路径
				String newPath = aim + "\\" + newName + "." + ename;//?????
				
				count++;
				// 从起始文件复制到目标文件
				copy(file.get(i), newPath);
				System.out.println(newName + " ---复制成功");
			}

		}
		System.out.println("\n从目录" + start + "总共复制了" + count  + "个文件"
				);

	}

	// 用来存放满足指定后缀名的所有文件的路径
	List<String> list = new ArrayList<String>();

	/*
	 * 查找文件夹里的所有相同扩展名的文件 传入文件夹目录和后缀名,返回list类型的符合条件的文件路径
	 */
	public List<String> outputFiles(String path, String lastname) {
		// 将路径转成一个文件类型
		File file = new File(path);
		// 返回目录中的文件 或者文件夹
		File[] files = file.listFiles();
		// 遍历文件或者文件夹
		for (File f : files) {
			// 判断是否是文件夹
			if (f.isDirectory()) {
				// 如果是文件夹则重新传入outputFile遍历
				outputFiles(f.toString(), lastname);
			} else if (lastname.equals("*")
					|| f.getName().endsWith("." + lastname)) {
				// 将完整的文件路径放入list容器里
				list.add(f.getPath());
			}
			// 统计遍历的文件个数
			counter++;
		}
		// 返回所有符合条件的文件路径集
		return list;
	}

	/*
	 * 要复制的文件夹,和要复制到的目标文件夹 传入起始路径和目标路径
	 */
	public void copy(String start, String aim) {
		int a = 0;
		FileInputStream in = null;
		FileOutputStream out = null;
		try {
			in = new FileInputStream(start);
			out = new FileOutputStream(aim);
			// 读起始路径文件
			while ((a = in.read()) != -1) {
				// 写入到目标路径文件
				out.write(a);
			}
			// 关闭通道
			in.close();
			out.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	
}
最近下载更多
wjdxsj  LV1 2020年11月2日
chenqiwax  LV4 2019年8月2日
han108  LV16 2019年3月6日
飞翔的企鹅嗯嗯  LV11 2019年1月30日
localhost1  LV1 2018年7月4日
都是老虎啊  LV12 2018年6月25日
925386146  LV1 2017年8月8日
wade123  LV31 2014年11月7日
最近浏览更多
heqian  LV16 2023年1月10日
任家毅  LV1 2021年6月1日
微量的记忆  LV10 2021年4月12日
wjdxsj  LV1 2020年11月2日
yhj123  LV2 2020年6月19日
jsjlvpan  LV1 2019年9月18日
废人a123456789 2019年8月6日
暂无贡献等级
chenqiwax  LV4 2019年8月2日
cheeseni  LV1 2019年7月6日
飞翔的企鹅嗯嗯  LV11 2019年1月30日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友