package test;
/**
- 利用文件输入输出流对文件夹进行复制
- @author
*/
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 a1 {
public static void main(String[] args) {
File f = new File("testDir");
File f2 = new File("test");
dirCopy(f, f2);
}
//文件夹的拷贝
public static void dirCopy(File dir,File dircopy) {
if(!dircopy.exists()) {
dircopy.mkdirs();
}
if(null != dir || dir.exists()) {
for(File a:dir.listFiles()) {
if(a.isFile()) {
copy(dir+a.separator+a.getName(), dircopy+a.separator+a.getName());
}else {
dirCopy(new File(dir+a.separator+a.getName()), new File(dircopy+a.separator+a.getName()));
}
}
}
}
//文件的拷贝
public static void copy(String s1,String s2) {
File f = new File(s1);
File f2 = new File(s2);
InputStream is = null;
OutputStream os = null;
try {
is = new FileInputStream(f);
os = new FileOutputStream(f2);
byte[] temp = new byte[1024];
int len = -1;
while((len = is.read(temp)) != -1) {
os.write(temp,0,len);
}
os.flush();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
if(null != os) {
os.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
if(null != is) {
is.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}