private static void copyRecursive(File srcFolder, File desFolder, BufferedWriter bw, int level) {
// One time this function called, it is one level deeper.
level++;
for(File file : srcFolder.listFiles()) {
// Depth first write the copy information to the record file.
writeIndent(bw, level);
try {
if(file.isDirectory()) {
bw.append("+");
}
bw.append(file.getName());
bw.newLine();
} catch (IOException e) {
e.printStackTrace();
}
if(file.isDirectory()) { // Create sub folder.
String subDesFolderPath = desFolder.getAbsolutePath()
+ "\\" + file.getName();
File subDesFolder = new File(subDesFolderPath);
subDesFolder.mkdir();
// Recursive copy the subfolder.
copyRecursive(file, subDesFolder, bw, level);
} else { // Do copy.
String desFilePath = desFolder.getAbsolutePath()
+ "\\" + file.getName();
try {
OutputStream os = new FileOutputStream(desFilePath);
InputStream is = new FileInputStream(file);
int byteread = 0;
byte[] buffer = new byte[1024];
while((byteread = is.read(buffer)) != -1) {
os.write(buffer, 0, byteread);
}
os.close();
is.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}