Find files with keywords in the specified directory using Java

Source: Internet
Author: User
Tags eol

Recently to change a product to the MySQL database, the source code is left behind, there are many anti-compilation files, and with errors. So you want to remove these anti-compilation files and avoid interference. Fortunately, the contents of these files have anti-compiler information. Through the keyword search under eclipse found a few of the paper, helpless had to find ways to delete the bulk. First think of using the Scripting language (BAT), online search, no suitable, and finally decided to use Java implementation.

1  Public Staticstring[] FindFiles (String baseurl,string ... contains) {2File BaseDIR =NewFile (BaseURL);3         if(!basedir.isdirectory ()) {4             Throw NewRuntimeException (BaseURL + "is nor a dir!");5         }6 AB (BaseDIR);7         return NULL;8     }9 Ten     Private Static voidAB (File BaseDIR) { OneFile[] Farr =basedir.listfiles (); A          -List<file> flist =NewArraylist<file>(); -         Try { theFlist =arrays.aslist (Farr); -}Catch(Exception e) { - e.printstacktrace (); -         } +Flist = Flist.stream (). Filter (f->NewMypredicate (). Test (f)). Collect (Collectors.tolist ());//dir left -         if(Flist.size () >0){ +              for(File f:flist) { A AB (f); at             } -         } -     } -      -      Public Static voidMain (String args[]) { -Finder.findfiles ("D:/work/workspace2/bsp2/src", "" "); in}

Using the JAVA8 Stream concurrency feature filter file list, now learn to sell ....

1  PackageMe.ks.main;2 3 ImportJava.io.File;4 Importjava.util.function.Predicate;5 6  Public classMypredicateImplementsPredicate<file> {7 8 @Override9      Public BooleanTest (File t) {Ten         if(T.isdirectory ()) { One             return true; A         } -         if(T.getname (). EndsWith (". Java")) { -             Try { theStringBuilder sb = Randomaccessfiletest.readlastoffile (T, 4); -                 if(Sb.tostring (). Contains ("Jd-core")) { -System.out.print (T.getabsolutepath () + ";"); -                 } +}Catch(Exception e) { - e.printstacktrace (); +             } A         } at         return false; -     } -  -}

The Anti-compilation tool is Jd-gui, the compiled file at the end of the jd-core words, so to find out whether the end of the file contains Jd-core words, code from the Internet, slightly modified:

1      Public StaticStringBuilder Readlastoffile (File F,intLastlinecount) {  2           3Randomaccessfile randomaccess =NULL;4StringBuilder SB =NULL;5         Try {6Randomaccess =NewRandomaccessfile (F, "R"); 7             BooleanEOL =false; 8             intc =-1; 9             LongFilelength =randomaccess.length (); Ten             LongSize = 1;  One             intLineCount = 0; AWw: while(!EOL) {   -                 Longoffset = filelength-(size++);  - Randomaccess.seek (offset);  the                 Switch(c =Randomaccess.read ()) {   -                  Case-1:   -                  Case' \ n ':   -                  Case' \ r ':   +Randomaccess.seek (offset + 1);  -                     if(++linecount==Lastlinecount) { +                              Breakww;  A                     } at                 }   -             }   -    -SB =NewStringBuilder ();  - String Line; -              while(line = Randomaccess.readline ())! =NULL) {   in Sb.append (line); -             } to}Catch(FileNotFoundException e) { + e.printstacktrace (); -}Catch(IOException e) { the System.err.println (F.getabsolutepath ()); * e.printstacktrace (); $}finally{Panax Notoginseng             Try{ -                 if(randomaccess!=NULL){ the Randomaccess.close ();  +                 } A}Catch(Exception e) {} the         } +         returnsb; -}

File list hit the console, more lazy, copy out and do more than the bulk delete ing ....

Find files with keywords in the specified directory using Java

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.