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();
}
}
}
}