import java.io.*;
public class CopyFolder {
public static void copyFolder(String oldPath, String newPath) throws IOException {
File file = new File(oldPath);
FileInputStream fis = null;
FileOutputStream fos = null;
if(file.exists()) {
String[] fileList = file.list();
for( int i=0; i<fileList.length; i++) {
if((new File(oldPath+File.separator+fileList[i])).isFile()) {
int num = 0;
fis = new FileInputStream(oldPath+File.separator+fileList[i]);
fos = new FileOutputStream(newPath+File.separator+fileList[i]);
byte[] bt = new byte[1024];
while((num=fis.read(bt))!=-1) {
fos.write(bt, 0, num);
}
}
else {
String oldPath1 = oldPath+File.separator+fileList[i];
String newPath1 = newPath+File.separator+fileList[i];
File file1=new File(newPath1);
file1.mkdir();
copyFolder(oldPath1,newPath1);
}
}
}
else {
System.out.println("not exist folder");
}
fos.close();
fis.close();
}
public static void main(String[] args) throws IOException {
String oldPath="f:/java/doc/newFolder";
String newPath="f:/java/doc/copyFolder";
copyFolder(oldPath,newPath);
}
}
public class CopyFolder {
public static void copyFolder(String oldPath, String newPath) throws IOException {
File file = new File(oldPath);
FileInputStream fis = null;
FileOutputStream fos = null;
if(file.exists()) {
String[] fileList = file.list();
for( int i=0; i<fileList.length; i++) {
if((new File(oldPath+File.separator+fileList[i])).isFile()) {
int num = 0;
fis = new FileInputStream(oldPath+File.separator+fileList[i]);
fos = new FileOutputStream(newPath+File.separator+fileList[i]);
byte[] bt = new byte[1024];
while((num=fis.read(bt))!=-1) {
fos.write(bt, 0, num);
}
}
else {
String oldPath1 = oldPath+File.separator+fileList[i];
String newPath1 = newPath+File.separator+fileList[i];
File file1=new File(newPath1);
file1.mkdir();
copyFolder(oldPath1,newPath1);
}
}
}
else {
System.out.println("not exist folder");
}
fos.close();
fis.close();
}
public static void main(String[] args) throws IOException {
String oldPath="f:/java/doc/newFolder";
String newPath="f:/java/doc/copyFolder";
copyFolder(oldPath,newPath);
}
}