package demo;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;import java.nio.channels.FileChannel; import java.text.parseexception;import java.text.simpledateformat;import java.util.arraylist; import java.util.calendar;import java.util.date;public class demo {private static string str = "";p rivate static arraylist<string> filelist = new ArrayList<String> ();p Ublic static void main (String[] args) {string[] s = {"Years", "Friends", "Youth"}; getfiles ("E://duzhe", s);} By recursively getting all directories and their files under a certain path Public static void getfiles (string filepath,string[] s) {file root = new file (FilePath); &nbsP; file[] files = root.listfiles (); for (File file:files) { if (File.isdirectory ()) { // System.out.println ("[Folder]" +file.getabsolutepath ()); getfiles (File.getabsolutepath (), s); }else{ system.out.println ("[File]" +file.getabsolutepath ()); //character Matching if (File.length () < 1024*1024*2 && file.getname (). EndsWith (". txt")) { str = readtxt (file); if (Containstr (str,s) >=3) { system.out.println ("[" + File.getabsolutepath () + "]"); copys (file); } } } }}public static int containstr (string str,string[] s] { int ts= 0; for (int i = 0; i < s.length; i++) { if (Str.contains (s[i)) { ts++; //system.out.println ("string" +s[i]+ "In" + "specify string str!"); } } return ts; }public static string readtxt (file file) { string str = ""; try { string encoding= "GBK"; &Nbsp; if (File.isfile () && file.exists ()) { //determine if a file exists inputstreamreader read = new inputstreamreader (New fileinputstream (file), encoding);//considering the encoding format bufferedreader bufferedreader = new bufferedreader (Read); String lineTxt = null; while ((lineTxt = bufferedreader.readline ()) != null) { //system.out.println (LINETXT); str += linetxt; } read.close (); }else{ &NBSP;SYSTEM.OUT.PRINTLN ("The specified file cannot be found"); } } catch (exception e) { &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SYSTEM.OUT.PRINTLN ("Error reading file contents"); e.printstacktrace (); } return str; }//Copy File Public static void copys (File f) {try { &nBsp; filechannel in = new fileinputstream (f ). Getchannel (), out = new fileoutputstream ("e:\\fly\\" +f.getname ()). GetChannel (); in.transferto (0, in.size (), out);//simplified notation Out.transferfrom (In, 0, in.size ());//Effect ditto } catch (filenotfoundexception e) { &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SYSTEM.OUT.PRINTLN ("File not Found"); } catch (ioexception e) { e.printstacktrace (); } }}
All hierarchy files are included in the recursive folder with multiple keywords, and then the files are copied