一、递归的概念
程序调用自身的编程技巧称为递归。
递归的核心思想就是将一个大规模复杂的问题层层转化为一个与原问题相似的规模较小的问题来求解。
二、递归的优点
使用递归的好处是只需要少量的代码就可以描述出求解问题过程中多次重复的计算,大大减少了程序的代码量。
三、递归的缺点
在时间和空间的复杂度上往往不是最优的。
四、实现递归的条件
一般来说,递归要有边界、递归前进段和递归返回段。当不满足边界条件时,递归前进,当满足边界条件时,递归返回。
五、示例
使用递归将一个文件夹中的所有文件拷贝到另外一个文件夹
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;/** 使用递归将一个文件夹中的所有文件拷贝到另外一个文件夹*/
public class Recursion {public final String srcPath;public final String targetPath;public Recursion(String srcPath, String targetPath) {this.srcPath = srcPath;this.targetPath = targetPath;}public String getSrcPath() {return srcPath;}public String getTargetPath() {return targetPath;}public static void main(String[] args) throws IOException {Recursion recursion = new Recursion("F:\\固态盘之前\\MyWorkSpace\\MyEclipse 9\\day13","C:\\Users\\cc\\Desktop\\day13");recursion.iteratorFile(recursion.getSrcPath());}/** 递归方法*/public void iteratorFile(String srcPath) {if (srcPath != null && !srcPath.equals("")) {File file = new File(srcPath);if (file.isDirectory()) {File[] files = file.listFiles();//遍历当前目录下的所有文件和目录for (int i = 0; i < files.length; i++) {if (files[i].isDirectory()) {iteratorFile(files[i].getAbsolutePath());} else {//开启一个线程拷贝文件new ThreadCopy(files[i].getAbsolutePath(),files[i].getAbsolutePath().replace(this.getSrcPath(), this.getTargetPath())).start();}}} else {//开启一个线程拷贝文件new ThreadCopy(file.getAbsolutePath(),file.getAbsolutePath().replace(this.getSrcPath(), this.getTargetPath())).start();}}}
}class ThreadCopy extends Thread {private String src;private String tar;public ThreadCopy(String src, String tar) {this.src = src;this.tar = tar;}@Overridepublic void run() {FileInputStream fis = null;BufferedInputStream bis = null;FileOutputStream fos = null;BufferedOutputStream bos = null;try {File file = new File(tar);//如果拷贝文件的目标目录不存在,则创建目标目录File directory = new File(file.getParent());if (!directory.exists()) {directory.mkdirs();}fis = new FileInputStream(src);bis = new BufferedInputStream(fis);fos = new FileOutputStream(tar);bos = new BufferedOutputStream(fos);byte[] buffer = new byte[1024];int len = 0;while ((len = bis.read(buffer)) != -1) {bos.write(buffer, 0, len);}bos.flush();System.out.println(Thread.currentThread().getName() + ":拷贝完了!");} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {//关闭流close(bos, fos);close(bis, fis);}}public void close(OutputStream oStream1, OutputStream oStream2) {if (oStream1 != null) {try {oStream1.close();} catch (IOException e) {e.printStackTrace();}}if (oStream2 != null) {try {oStream2.close();} catch (IOException e) {e.printStackTrace();}}}public void close(InputStream iStream1, InputStream iStream2) {if (iStream1 != null) {try {iStream1.close();} catch (IOException e) {e.printStackTrace();}}if (iStream2 != null) {try {iStream2.close();} catch (IOException e) {e.printStackTrace();}}}}