java 文件夹copy 。copy下面所有的文件及其文件夹 。

头脑不清醒,希望得到源码
FolderCopy(String src,String des){
。。。。
if(文件夹)创建文件夹,继续下一层
else 复制文件
。。。。
}

第1个回答  推荐于2016-07-10
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
public class FolderCopy {
/**
* @param args
*/
/*文件copy
* param src,des
* return ture 成功。false 失败
*/
public static boolean fileCopy(String src,String des){
File srcFile=new File(src);
File desFile=new File(des);
byte[]b=new byte[1024];
String string="";
try {
FileInputStream fis=new FileInputStream(srcFile);
FileOutputStream fos=new FileOutputStream(desFile,false);
while(true){
int i=fis.read(b);
if(i==-1)break;
fos.write(b,0,i);
}
fos.close();
fis.close();
return true;
} catch (Exception e){
e.printStackTrace();
}
return false;
}

/*
* 文件夹copy
* param src,des
* return ture 成功。false 失败
*
*/

public static boolean folderCopy(String src,String des){
File srcFile=new File(src);
File desFile=new File(des);
File []files=srcFile.listFiles();
boolean flag = false;
if(!desFile.exists())desFile.mkdir();
for(int i=0;i<files.length;i++){
String path=files[i].getAbsolutePath();
if(files[i].isDirectory()){
File newFile=new File("path.replace(src,des)");
if(!newFile.exists())newFile.mkdir();//不存在新建文件夹
folderCopy(path,path.replace(src,des));
}
else
flag=fileCopy(path,path.replace(src,des));//文件复制函数
}
return flag;
}
public static void main(String[] args) {
FolderCopy.folderCopy("d:\\1","C:\\1" );

}

}
希望能够帮助你。本回答被提问者采纳
第2个回答  2012-04-15
apache commons IO 包
FileUtils 类追问

老师告诉我们了,先让我们自己写。

追答

用递归