场景:要将 D 盘上的某个文件夹通过 IO 流复制到 E 盘下的某个文件夹中。
实现:
在一个 文件工具类中,通过 IO 流技术,运用递归算法,实现了一个简单的磁盘上文件夹的复制
代码:
public class FileUtil {
/**
* 复制目录
*
* @param srcFile 源文件
* @param destFile 目标文件
*/
public static void copyDir(File srcFile, File destFile) throws Exception{
// 判断是否为文件
if (srcFile.isFile()) {
FileInputStream fis = new FileInputStream(srcFile);
FileOutputStream fos = new FileOutputStream((destFile.getAbsolutePath().endsWith("\\") ? destFile.getAbsolutePath()
: destFile.getAbsolutePath() + "\\") + srcFile.getAbsolutePath