将指定类型的文件复制到指定目录

文章讲述了如何使用Java编写CopyDir类,该类可以递归地将指定目录及其文件(包括.md文件且含有中文的)按照原有目录结构复制到目标目录,同时提供了一个辅助方法isContainsChinese用于检查字符串中是否包含中文。
摘要由CSDN通过智能技术生成
package com.dang.mycharset;

import java.io.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class CopyDir {
    public static String destPath = "E:\\BaiduNetdiskDownload\\java_md\\";
    public static void main(String[] args) throws IOException {
        String toPath = "F:\\java_test_md\\";
        String fromPath = "F:\\最新版java学习路线\\";
        File file = new File(fromPath);
        //将指定目录下的指定文件,按照原先目录结构拷贝到新目录
        File dest = new File(toPath);
        copyDir(file, dest);
          //将指定目录下的指定文件拷贝到统一目录下
//        copyDirToDestDir(file);
    }

    /*
     *  将指定目录文件,按照原先目录结构拷贝到新目录
     * 参数一:文件原目录
     * 参数二:要拷贝到的新目录
     */
    public static void copyDir(File src, File dest) throws IOException {
//        dest.mkdirs();//创建目录,目录存在则失败;目录不存在,则直接创建

        //递归
        //1、进入数据源
        File[] files = src.listFiles();
        //2、遍历数组
        if (files != null) {//需要对文件夹进行非空判断
            for (File file : files) {
                String filename = file.getName();
                if (file.isFile()) {
                    //3、判断文件,拷贝
                    if (filename.endsWith(".md") && isContainsChinese(filename)) {
                        dest.mkdirs();//创建目录,目录存在则失败;目录不存在,则直接创建
                        FileInputStream fis = new FileInputStream(file);
                        FileOutputStream fos = new FileOutputStream(new File(dest, filename));
                        byte[] bytes = new byte[1024];
                        int len;
                        while ((len = fis.read(bytes)) != -1) {
                            fos.write(bytes, 0, len);//每次读取多少就写入多少
                        }
                        //释放资源
                        //规则:先开的最后关闭
                        fos.close();
                        fis.close();
                    }
                } else {
                    //4、判断文件夹,递归
                    copyDir(file, new File(dest, filename));
                }
            }
        }
    }
    /*
     * 将指定目录及子目录下的指定文件,直接拷贝到新目录
     */
    public static void copyDirToDestDir(File src) throws IOException {
        //递归
        //1、进入数据源
        File[] files = src.listFiles();
        //2、遍历数组
        if (files != null) {//需要对文件夹进行非空判断
            for (File file : files) {
                String filename = file.getName();
                if (file.isFile()) {
                    //3、判断文件,拷贝
                    if (filename.endsWith(".md") && isContainsChinese(filename)) {
                        FileInputStream fis = new FileInputStream(file);
                        FileOutputStream fos = new FileOutputStream(destPath+filename);
                        byte[] bytes = new byte[1024];
                        int len;
                        while ((len = fis.read(bytes)) != -1) {
                            fos.write(bytes, 0, len);//每次读取多少就写入多少
                        }
                        //释放资源
                        //规则:先开的最后关闭
                        fos.close();
                        fis.close();
                    }
                } else {
                    //4、判断文件夹,递归
                    copyDirToDestDir(file);
                }
            }
        }
    }

    /**
     * 判断字符串中是否包含中文
     *
     * @param str 待校验字符串
     * @return 是否为中文
     * @warn 不能校验是否为中文标点符号
     */
    public static boolean isContainsChinese(String str) {
        if (str == null) {
            return false;
        }
        Pattern p = Pattern.compile("[\u4e00-\u9fa5]");
        Matcher m = p.matcher(str);
        return m.find();
    }
}

  • 11
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值