java 文件复制 文件夹复制工具类

自己写的工具类,有不对的地方或者可以优化的地方,希望留言,共同讨论


import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class CopyFileDirUtil {
	public static  int IS_EXISTS  = 0;
	public static void main(String[] args) {
//		String sourcePath = "D:/study_project/java-io-test.txt";
//		String targetPath = "D:/study_project/java-io-test-to.txt";
		String sourcePath = "D:/study_project/test";
		String targetPath = "D:/study_project/copyTest";
//		copyFile(sourcePath,targetPath);
		copyDirectory(sourcePath, targetPath);
	}
	/**
	 * 文件的拷贝
	 * @param sourcePath
	 * @param targetPath
	 */
	public static void copyFile(String sourcePath,String targetPath){
		File sourceFile = new File(sourcePath);
		File targetFile = new File(targetPath);
		copyFile(sourceFile, targetFile);
	}
	/**
	 * 文件的拷贝
	 * @param sourceFile
	 * @param targetFile
	 */
	public static void copyFile(File sourceFile,File targetFile){
		if(!sourceFile.exists()){
			System.out.println("源文件没有找到");
			return;
		}
		if(targetFile.exists()){
			System.out.println("目标文件已经存在,删除已存在的目标文件");
			targetFile.delete();
		}
		BufferedInputStream fis = null;
		BufferedOutputStream fos = null;
		int len = 100;
		int count = -1;
		byte[] buffer = new byte[len];
		try {
			fis = new BufferedInputStream(new FileInputStream(sourceFile));
			fos = new BufferedOutputStream(new FileOutputStream(targetFile));
			while ((count=fis.read(buffer, 0, len))!= -1) {
				fos.write(buffer, 0, count);
			}
			fos.flush();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			System.out.println("文件没有找到");
		} catch (IOException e) {
			e.printStackTrace();
			System.out.println("文件读取异常");
		} finally {
			try {
				fis.close();
				fos.close();
			} catch (Exception e) {
				System.out.println("流关闭异常");
			}
		}
	}
	/**
	 * 文件夹拷贝
	 * @param sourcePath
	 * @param targetPath
	 */
	public static void copyDirectory(String sourcePath,String targetPath){
		File sourceFile = new File(sourcePath);
		File targetFile = new File(targetPath);
		copyDirectory(sourceFile, targetFile);
	}
	/**
	 * 文件夹拷贝
	 * @param sourcePath
	 * @param targetPath
	 */
	public static void copyDirectory(File sourceFile,File targetFile){
		if(!sourceFile.exists()){
			System.out.println("源文件没有找到");
			return;
		}
		if(targetFile.exists() && IS_EXISTS == 0){
			System.out.println("目标文件已经存在,删除已存在的目标文件");
			targetFile.delete();
			IS_EXISTS = 1;
		}
		if(sourceFile.isDirectory()){
			targetFile.mkdir();
		}
		if(sourceFile.isDirectory()){
			File[] listFile = sourceFile.listFiles();
			for(File file : listFile){
				String newPath = file.getPath();
				String newCopyPath = targetFile.getPath()+File.separator+file.getName();
				if(file.isDirectory()){
					copyDirectory(newPath, newCopyPath);
				}else{
					copyFile(newPath, newCopyPath);
				}
			}
		}else{
			copyFile(sourceFile, targetFile);
		}
	}

}

拷贝文件和文件夹增强健壮性,修改优化后的结果如下:


import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class CopyUtil {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		String source = "D:/2";//源
		String target = "D:/3";//目标
		File sou = new File(source);
		File tar = new File(target);
		if(sou.isDirectory()){
			//在目标文件下,创建源文件文件夹
			tar = new File(tar,sou.getName());
		}
		//拷贝文件
		copyDir(sou, tar);
	}
	/**
	 * 拷贝文件夹
	 * @param source
	 * @param target
	 */
	public static void copyDir(File source,File target){
		if(source.isFile()){//如果源是一个文件
			//拷贝文件
			copyFile(source,target);
		}else if(source.isDirectory()){//如果源是一个文件夹
			//目标下创建文件夹
			target.mkdirs();
			//递归循环查找该文件夹下的所有文件和文件夹
			for(File sub : source.listFiles()){
				copyDir(sub,new File(target,sub.getName()));
			}
		}
	}
	/**
	 * 拷贝文件
	 * @param sourceFile
	 * @param targetFile
	 */
	public static void copyFile(File sourceFile,File targetFile){
		
		if(targetFile.exists()){
			System.out.println("目标文件已经存在,删除已存在的目标文件");
			targetFile.delete();
		}
		BufferedInputStream fis = null;
		BufferedOutputStream fos = null;
		int len = 100;
		int count = -1;
		byte[] buffer = new byte[len];
		try {
			fis = new BufferedInputStream(new FileInputStream(sourceFile));
			fos = new BufferedOutputStream(new FileOutputStream(targetFile));
//			fis.read(buffer, 0, len) 每次最多读取len这个长度的数据,放入到buffer自己数据中,赋值给变量count,如果数据读取完毕,count=-1,作为推出循环的条件
			while ((count=fis.read(buffer, 0, len))!= -1) {
//				缓冲写出数据
				fos.write(buffer, 0, count);
			}
//			强制刷新数据到磁盘
			fos.flush();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			System.out.println("文件没有找到");
		} catch (IOException e) {
			e.printStackTrace();
			System.out.println("文件读取异常");
		} finally {
			try {
				fis.close();
				fos.close();
			} catch (Exception e) {
				System.out.println("流关闭异常");
			}
		}
	}

}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值