//使用IO流实现文件复制
public static void copy(String by,String to) throws IOException {
File fileby = new File(by);
File fileto = new File(to);
BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(fileby));
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(fileto));
byte[] bytes = new byte[1024];
int i;
while ((i = bufferedInputStream.read(bytes)) != -1){
bufferedOutputStream.write(bytes,0,i);
}
bufferedInputStream.close();
bufferedOutputStream.flush();
bufferedOutputStream.close();
}
/**
* 复制文件夹(使用缓冲字节流)
* @param sourcePath 源文件夹路径
* @param targetPath 目标文件夹路径
*/
public static void copyFolder(String sourcePath,String targetPath) throws Exception{
//源文件夹路径
File sourceFile = new File(sourcePath);
//目标文件夹路径
File targetFile = new File(targetPath);
if(!sourceFile.exists()){
throw new Exception("文件夹不存在");
}
if(!sourceFile.isDirectory()){
throw new Exception("源文件夹不是目录");
}
if(!targetFile.exists()){
targetFile.mkdirs();
}
if(!targetFile.isDirectory()){
throw new Exception("目标文件夹不是目录");
}
File[] files = sourceFile.listFiles();
if(files == null || files.length == 0){
return;
}
for(File file : files){
//文件要移动的路径
String movePath = targetFile+File.separator+file.getName();
if(file.isDirectory()){
//如果是目录则递归调用
copyFolder(file.getAbsolutePath(),movePath);
}else {
//如果是文件则复制文件
BufferedInputStream in = new BufferedInputStream(new FileInputStream(file));
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(movePath));
byte[] b = new byte[1024];
int temp = 0;
while((temp = in.read(b)) != -1){
out.write(b,0,temp);
}
out.close();
in.close();
}
}
}