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(); } } } }
java实现文件夹下的指定文件查找,及复制到指定文件夹
最新推荐文章于 2022-11-25 15:52:57 发布