前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >输入两个文件夹路径,把其中一个文件夹中(包含内容)拷贝到另一个文件夹中

输入两个文件夹路径,把其中一个文件夹中(包含内容)拷贝到另一个文件夹中

作者头像
砖业洋__
发布2023-05-06 17:16:16
4250
发布2023-05-06 17:16:16
举报
文章被收录于专栏:博客迁移同步

学习过程中自己记录一下

代码语言:javascript
复制
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;

public class Test3 {
    /*
     * 从键盘接收两个文件夹路径,把其中一个文件夹中(包含内容)拷贝到另一个文件夹中
     */
    static BufferedReader br;

    public static void main(String[] args) throws IOException {
        File src = getDir(); // 获取源文件
        File dest = getDir(); // 拷贝到目的文件夹中
        br.close();
        if (src.equals(dest)) {
            System.out.println("目标文件夹是源文件夹的子文件夹!");
        } else {
            copyFile(src, dest);
        }
        
    }

    public static File getDir() {
        try {
            // 不用自动关流写法,否则第二个路径无法输入
            br = new BufferedReader(new InputStreamReader(System.in));
            System.out.println("请输入一个文件夹路径");
            while (true) {
                String line = br.readLine();
                File dir = new File(line);
                if (!dir.exists()) {
                    System.out.println("不存在!重输:");
                } else if (dir.isFile()) {
                    System.out.println("输入的不是文件夹,请重新输入:");
                } else {
                    return dir;
                }
            }
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
        return null; 
    }

    /**
     * 拷贝带内容的文件夹 1.返回值类型void 2.参数列表File src, File dest
     * 
     * @throws IOException
     */
    public static void copyFile(File src, File dest) throws IOException {
        File newDir = new File(dest, src.getName());
        newDir.mkdirs();
        File[] subFiles = src.listFiles(); // 获取到原文件夹中所有的文件和文件夹
        for (File subFile : subFiles) {
            if (subFile.isFile()) {
                try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(subFile));
                        BufferedOutputStream bos = new BufferedOutputStream(
                                new FileOutputStream(new File(newDir, subFile.getName())));) {
                    int len;
                    while ((len = bis.read()) != -1) {
                        bos.write(len);
                    }
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            } else { // 否则是文件夹
                copyFile(subFile, newDir);
            }
        }
    }
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018-06-26,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档