Java实现文件拷贝

package cn.hnb.test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

public class FileUtil {
	/**
	 * 文件复制
	 * @param srcPath 源文件路径
	 * @param targetPath 复制后存放路径
	 * @throws Exception
	 */
	public static void copyFile(String srcPath, String targetPath) throws Exception {
		File srcFile = new File(srcPath);
		File target = new File(targetPath);
		if (!srcFile.exists()) {
			throw new Exception("文件不存在!");
		}
		if (!srcFile.isFile()) {
			throw new Exception("不是文件!");
		}
		//判断目标路径是否是目录
		if (!target.isDirectory()) {
			throw new Exception("文件路径不存在!");
		}

		// 获取源文件的文件名
		String fileName = srcPath.substring(srcPath.lastIndexOf("\\") + 1);
		//TODO:判断是否存在相同的文件名的文件
		File[] listFiles = target.listFiles();
		for (File file : listFiles) {
			if(fileName.equals(file.getName())){
				fileName += "_1";
			}
		}
		String newFileName = targetPath + File.separator + fileName;
		File targetFile = new File(newFileName);
		FileInputStream in = null;
		FileOutputStream out = null;
		try {
			in = new FileInputStream(srcFile);
			out = new FileOutputStream(targetFile);
			//从in中批量读取字节,放入到buf这个字节数组中,
			// 从第0个位置开始放,最多放buf.length个 返回的是读到的字节的个数
			byte[] buf = new byte[8 * 1024];
			int len = 0;
			while ((len = in.read(buf)) != -1) {
				out.write(buf, 0, len);
				out.flush();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			try{
				if(in != null){
				  in.close();
				}
			}catch(Exception e){
				 System.out.println("关闭输入流错误!");
			}
			try{
				if(out != null){
					out.close();
				}
			}catch(Exception e){
				System.out.println("关闭输出流错误!");
			}
		}

	}
	//测试
	public static void main(String[] args) throws Exception {
		copyFile("C:\\Users\\admin\\Desktop\\test\\1-2.txt", "C:\\Users\\admin\\Desktop\\test");
	}
}
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值