java实现文件夹下的指定文件查找,及复制到指定文件夹

package com.primeton;

import java.io.*;

public class FileFilter {

            public static void main(String[] args) throws Exception {
        //目标源文件夹
                File source = new File("D:\\software\\repository\\org\\apache\\hadoop");
                File fil = new File("C:\\Users\\my\\Desktop\\hadp_conf\\hadoop");
                //创建目标目录的file对象
                if (!fil.exists()) {
                    //不存在就创建文件夹
                    fil.mkdir();
                }

                System.out.println("复制成功");

                readfile(source, fil);
            }


            public static void readfile(File file, File fil) throws Exception {
                try {
                    if (!file.isDirectory()) {
                        File writefile = new File(fil + File.separator + file.getName());
                        outFile(file, writefile);
                    } else if (file.isDirectory()) {
                        File[] files = file.listFiles();
                        for (File readfile : files) {
                            File writefile = new File(fil + File.separator + readfile.getName());
                            if (!readfile.isDirectory()) {
                                outFile(readfile,writefile);
                            } else if (readfile.isDirectory()) {
                                readfile(readfile ,fil);
                            }
                        }

                    }

                } catch (FileNotFoundException e) {
                    System.out.println("readfile()   Exception:" + e.getMessage());
                }
            }

            private static void outFile(File readfile,File fil) throws Exception{
                if (readfile.isFile()) {
                    if (readfile.getName().endsWith("2.9.2.jar")){
                        System.out.println(readfile.getName());
                        FileInputStream in = new FileInputStream(readfile);
                        FileOutputStream out = new FileOutputStream(fil);

                        byte[] bs = new byte[1024];

                        int count = 0;
                        //循环把源文件的内容写入新文件
                        while ((count = in.read(bs, 0, bs.length)) != -1) {
                            out.write(bs, 0, count);
                        }
                        //关闭流
                        out.flush();
                        out.close();
                        in.close();
                    }
                }
            }


}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值