All hierarchy files are included in the recursive folder with multiple keywords, and then the files are copied

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.