- package cn.itcast_05;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.IOException;
- public class CopyFileDemo {
- public static void main(String[] args) throws IOException {
- File srcFile=new File("D:\\aaa");
- File deskFile=new File("D:\\bbb");
- copyFolder(srcFile,deskFile);
- }
- private static void copyFolder(File srcFile, File deskFile) throws IOException {
- // TODO Auto-generated method stub
- if(deskFile.exists()){
- deskFile.mkdir();
- }
- File[] fileArray=srcFile.listFiles();
- for(File file:fileArray){
- if(file.isDirectory()){
- copyFolder(new File(srcFile,file.getName()),new File(deskFile,file.getName()));
- }else{
- FileOutputStream fos=new FileOutputStream(new File(deskFile,file.getName()));
- FileInputStream fis=new FileInputStream(new File(srcFile,file.getName()));
- byte[] bys=new byte[1024];
- for(int len=0;(len=fis.read(bys))!=-1;){
- fos.write(bys,0,len);
- }
- fis.close();
- fos.close();
- }
- }
-
- }
- }
复制代码 |
|