Advertisement
giancarloparma

StringMatchingOld

Nov 7th, 2018
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.96 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.File;
  3. import java.io.FileFilter;
  4. import java.io.FileReader;
  5. import java.io.IOException;
  6.  
  7. import org.slf4j.Logger;
  8. import org.slf4j.LoggerFactory;
  9. import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
  10. import org.springframework.context.annotation.Configuration;
  11.  
  12. @Configuration
  13. @EnableAutoConfiguration
  14. public class StringMatchingOld {
  15.     private static final Logger LOG = LoggerFactory.getLogger(StringMatchingOld.class);
  16.    
  17.     public void match(String match, String dirPath, String fileNamePrefix) {
  18.         LOG.info("Match=" + match);
  19.         LOG.info("DirPath=" + dirPath);
  20.         LOG.info("FileNamePrefix=" + fileNamePrefix);
  21.        
  22.         LOG.info("*****************START*****************");
  23.        
  24.         try {
  25.             long start = System.nanoTime();
  26.             final Counter fileCounter = new Counter();
  27.            
  28.             File inputDir = new File(dirPath);
  29.             File[] files = inputDir.listFiles(new FileFilter() {
  30.                
  31.                 @Override
  32.                 public boolean accept(File pathname) {
  33.                     return pathname.isFile() && pathname.getName().startsWith(fileNamePrefix);
  34.                 }
  35.             });
  36.            
  37.             for (File file : files) {
  38.                 LOG.info("Searching in " + file.getAbsolutePath() + " ...");
  39.                 BufferedReader br = new BufferedReader(new FileReader(file));
  40.                 String line = null;
  41.                 while ( (line = br.readLine()) != null) {
  42.                     if (line.contains(match)) {
  43.                         LOG.info("Found match in file=[" + file.getAbsolutePath() + "]");
  44.                         fileCounter.add();
  45.                         break;
  46.                     }
  47.                 }
  48.                
  49.                 br.close();
  50.             }
  51.            
  52.             LOG.info("Found " + fileCounter.get() + " files with match");
  53.             long end = System.nanoTime();
  54.             LOG.info("Time: " + (end - start)/1000000 +  " ms");
  55.         } catch (IOException e) {
  56.             throw new RuntimeException(e);
  57.         };
  58.        
  59.         LOG.info("*****************END*****************");
  60.     }
  61.    
  62.     private static final class Counter {
  63.         int counter = 0;
  64.        
  65.         public void add() {
  66.             counter++;
  67.         }
  68.        
  69.         public int get() {
  70.             return counter;
  71.         }
  72.     }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement