java 文件复制(java.io.File)

java 文件复制 

package com.ssh.common.util;
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;

/**
 * 文件复制util
 * @author byl
 * @date 2019年8月19日12:44:52
 */
public class FileCopyUtil {
 
	/**
	 * 复制文件到指定目录
	 * @param fromfile 文件(java.io.file)
	 * @param formFileName 目标文件名称
	 * @param toPath 新文件路径
	 * @param toFileName 新文件名称
	 */
	public static void readFile(File fromfile,String formFileName, String toPath,String toFileName) {
 
		//生产新文件全路径
		String newFile = toPath+toFileName;
		File tofile = new File(newFile);
 
		FileInputStream inputStream = null;
		FileOutputStream outputStream = null;
 
		BufferedInputStream bufferedInputStream = null;
		BufferedOutputStream bufferedOutputStream = null;
		try {
 
			inputStream = new FileInputStream(fromfile);
			outputStream = new FileOutputStream(tofile);
 
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		if (null != inputStream && null != outputStream) {
 
			bufferedInputStream = new BufferedInputStream(inputStream);
			bufferedOutputStream = new BufferedOutputStream(outputStream);
			byte[] b = new byte[1024];
			try {
				while (-1 != (bufferedInputStream.read(b))) {
					bufferedOutputStream.write(b);
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
 
		}
 
		if (null != bufferedInputStream) {
 
			try {
				bufferedInputStream.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		if (null != bufferedOutputStream) {
			try {
				bufferedOutputStream.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		if (null != inputStream) {
			try {
				inputStream.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		if (null != outputStream) {
			try {
				outputStream.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值