挑选盘里所有的相同后缀文件复制出来

本文介绍了一个使用Java实现的文件搜索与复制工具,能够遍历指定目录下的所有文件,筛选出特定类型的文件并将其复制到指定的目标目录。工具通过递归方式遍历文件夹,利用正则表达式匹配文件类型,支持自定义输入源目录、目标目录及文件类型。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package com.obcy;


import java.io.*;
import java.util.Scanner;

public class Search {
    private static Search search = new Search();
    private String source = "" ;
    private String target = "";
    private String type = "";

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        System.out.println("请输入你要遍历的目录(绝对路径)");
        search.source = sc.nextLine();
        System.out.println("请输入你要挑选的文件类型(文件后缀名)");
        search.type = sc.nextLine();
        System.out.println("你要将这种类型的文件复制到哪(结对路径)");
        search.target = sc.nextLine();
        System.out.println("startting");

        getAllPath(new File(search.source));
    }




    //此功能遍历allpath集合,并把string对象拆分,取到所有的文件类型

    private static String getPathTail(String filename) {

        String[] split = filename.split("\\.");
        return  split[split.length-1];


    }

    //复制文件方法
    public void cp(String input,String output) throws IOException {


        File in = new File(input) ;
        File dir = new File(output);

        if(!dir.exists()){
            if(dir.mkdirs()){
                System.out.println("已创建目录"+dir.getAbsolutePath());
            }else{
                System.out.println("目录创建失败");
                return;
            }
        }
        System.out.println(in.getName()+"正在复制");
        File out = new File(dir,in.getName());
        FileInputStream fin = new FileInputStream(in);
        FileOutputStream fout = new FileOutputStream(out);
        BufferedInputStream bfin = new BufferedInputStream(fin);
        BufferedOutputStream bfout = new BufferedOutputStream(fout);

        int len = 0;
        byte[] bt = new byte[4096];

        while ((len = fin.read(bt))!=-1){

            fout.write(bt,0,len-1);
        }
        System.out.println(in.getName()+"复制完成");

        fin.close();
        fout.close();
        bfin.close();
        bfout.close();


    }

    //递归遍历到文件夹下所有的文件,并存储到ArrayList数组内
    private static  void getAllPath(File path) {
        if (path.exists()) {         //判断路径是否存在

            if (path.isFile()) {         //判断是不是文件,是的话,得到其文件名。不是的话,就是目录
                String absolutePath = path.getAbsolutePath();
                String tail = getPathTail(absolutePath);
                //获取文件类型
                if(search.type.equals(tail)){

                    try {
                        search.cp(absolutePath,search.target);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                }

            }else{
                File[] files = path.listFiles();         //得到目录下所有的Fiel列表
                if (files!=null) {                //如果目录列表不为零,递归操作此列表里所有路径
                    for (File file : files) {
                        getAllPath(file);
                    }
                }



            }
        }else{
            System.out.println("文件路径不存在");
        }
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值