回头补了下io流的知识,写了一个文件夹的拷贝,这里记录一下
package top.temp.io;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class CopyFiles {
public static void main(String[] args) {
copyFiles("D:\\eclipse\\project\\test1\\src","D:\\eclipse\\project\\test1\\src2");
}
private static void copyFiles(String src1, String dest1) {
File src = new File(src1);
if(null == src || !src.exists()) {
return ;
}else {
if(src.isDirectory()) {
dest1 = (dest1 + File.separator +src.getName());
File dest = new File(dest1);
if(dest.mkdirs()) {
for(File f:src.listFiles()) {
copyFiles(f.getAbsolutePath(), dest1);
}
}else {
System.out.println("error");
}
}else {
copyFile(src1, dest1+File.separator+src.getName());
}
}
}
public static void copyFile(String src1,String dest1) {
File dest = new File(dest1);
File src = new File(src1);
OutputStream fis = null;
InputStream is = null;
try {
is = new FileInputStream(src);
fis = new FileOutputStream(dest,false);
byte[] flush = new byte[1024*10];
int temp = -1;
while((temp = is.read(flush)) != -1) {
fis.write(flush,0,temp);
}
fis.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
if(fis != null) {
fis.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if(is != null) {
is.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}