本帖最后由 杨强 于 2011-12-22 16:26 编辑
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Demo {
public static void main(String[] args) throws IOException {
File src = DirUtil.getDir();
File dest = DirUtil.getDir();
copyDir(src, dest);
}
private static void copyDir(File src, File dest) throws IOException {
File newDir = new File(dest, src.getName());
newDir.mkdirs();
File[] subFiles = src.listFiles();
for(File subFile : subFiles)
if (subFile.isDirectory())
copyDir(subFile, newDir);
else
copyFile(subFile, new File(newDir, subFile.getName()));
}
private static void copyFile(File src, File dest) throws IOException{
FileInputStream fis = new FileInputStream(src);
FileOutputStream fos = new FileOutputStream(dest);
byte[] buf = new byte[1024];
int len;
while((len = fis.read(buf)) != -1)
fos.write(buf, 0, len);
fis.close();
fos.close();
}
}
|