package com.lingshang.io.byteIO;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class CopyFileUtils {
// 文件的拷贝
public static void copyFile(String srcpath,String destpath) throws IOException {
// TODO Auto-generated method stub
copyFile(new File(srcpath), new File(destpath));
}
// 文件的拷贝
public static void copyFile(File srcpath,File destpath) throws IOException {
// TODO Auto-generated method stub
if(!srcpath.isFile()){
System.out.println(“只能拷贝文件”);
throw new IOException(“只能拷贝文件”);
}
InputStream is = new FileInputStream(srcpath);
OutputStream os = new FileOutputStream(destpath);
byte[] flush = new byte[1024];// 一次读取多少字节数
int len = 0;
while (-1 != (len = is.read(flush))) {
os.write(flush, 0, len);
}
os.flush();
os.close();
is.close();
}
// 文件夹的拷贝
public static void copyDir(String srcpath, String destpath) {
File src = new File(srcpath);
File dest = new File(destpath);
copyDir(src, dest);
}
public static void copyDir(File src, File dest) {
if (src.isDirectory()) {
dest = new File(dest, src.getName());
}
copyDirDetal(src, dest);
}
private static void copyDirDetal(File src, File dest) {
// TODO Auto-generated method stub
if (src.isFile()) {
try {
CopyFileUtils.copyFile(src, dest);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("拷贝文件失败");
}
} else if (src.isDirectory()) {
dest.mkdirs();
for (File sub : src.listFiles()) {
copyDirDetal(sub, new File(dest, sub.getName()));
}
}
}
}