码迷,mamicode.com
首页 > 编程语言 > 详细

星涛:采用java递归复制文件夹

时间:2020-10-06 21:05:22      阅读:28      评论:0      收藏:0      [点我收藏+]

标签:system   row   dwr   ack   fileread   package   exception   sys   copyfile   

package com.botao;

import java.io.*;

/**
 * @author cbt28
 */
public class FileUtil {
    public static String a="";
    public static String b="";

    public static void copyDir(File src, File target) throws IOException {
        if (!target.exists()) {
            target.mkdir();
        }
        File[] lf = src.listFiles();
        for (File file : lf) {
            if (file.isFile()) {
                System.out.println(src + "\\" + file.getName() + "--->" + target + "\\" + file.getName());
                boolean b = copyFile(new File(src + "\\" + file.getName()), new File(target + "\\" + file.getName()));
                System.out.println(b);
            } else {
                copyDir(new File(src + "\\" + file.getName()), new File(target + "\\" + file.getName()));
            }
        }
    }

    public static boolean copyFile(File src, File target) throws IOException {
        FileReader fr = new FileReader(src);
        BufferedReader br = new BufferedReader(fr);

        FileWriter fw = new FileWriter(target);
        BufferedWriter bw = new BufferedWriter(fw);
        String s = br.readLine();
        while (s != null) {
            bw.write(s);
            bw.newLine();
            s = br.readLine();
        }
        bw.close();
        fw.close();
        br.close();
        fr.close();
        return true;
    }
}

星涛:采用java递归复制文件夹

标签:system   row   dwr   ack   fileread   package   exception   sys   copyfile   

原文地址:https://www.cnblogs.com/botaoJava/p/13773259.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!