public classcopyDirectoryDemo {public static voidmain(String[] args) {
File srcFolder= new File("C:\\Users\\MA\\Desktop\\IOtest");
File destFolder= new File("C:\\Users\\MA\\Desktop\\IOtest\\test");
fun(srcFolder, destFolder);
}public static voidfun(File srcFolder, File destFolder) {
File[] fileArray=srcFolder.listFiles();if (!destFolder.exists()) {
destFolder.mkdir();
}for(File file : fileArray) {if(file.isDirectory()) {
String folderName=file.getName();
File newDestFolder= newFile(destFolder, folderName);
fun(file, newDestFolder);
}else{
String fileName=file.getName();
File destFile= newFile(destFolder, fileName);
copy(file, destFile);
}
}
}public static voidcopy(File file, File destFile) {
BufferedInputStream bis= null;
BufferedOutputStream bos= null;try{
bis= new BufferedInputStream(newFileInputStream(file));
bos= new BufferedOutputStream(newFileOutputStream(destFile));byte[] bys = new byte[1024];int len = 0;while((len=bis.read(bys))!=-1){
bos.write(bys,0,len);
}
}catch(FileNotFoundException e) {
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}finally{if(bis!=null){try{
bis.close();
}catch(IOException e) {
e.printStackTrace();
}
}if(bos!=null){try{
bos.close();
}catch(IOException e) {
e.printStackTrace();
}
}
}
}
}