Advertisement
giancarloparma

StringMatching

Nov 7th, 2018
388
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.93 KB | None | 0 0
  1. import java.io.IOException;
  2. import java.nio.charset.Charset;
  3. import java.nio.file.Files;
  4. import java.nio.file.Paths;
  5. import java.util.stream.Stream;
  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 StringMatching {
  15.     private static final Logger LOG = LoggerFactory.getLogger(StringMatching.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.         LOG.info("Threads: " + Runtime.getRuntime().availableProcessors());
  25.        
  26.         try {
  27.             long start = System.nanoTime();
  28.             final Counter fileCounter = new Counter();
  29.             Files.newDirectoryStream(Paths.get(dirPath), p -> p.getFileName().toString().startsWith(fileNamePrefix) && !Files.isDirectory(p)).
  30.                 forEach(filePath -> {
  31.                     LOG.info("Searching in " + filePath + " ...");
  32.                     try (Stream<String> lines = Files.lines(filePath, Charset.defaultCharset())) {
  33.                         lines.filter(line -> line.contains(match)).findAny().
  34.                             ifPresent(matchingLine -> {
  35.                                 LOG.info("Found match in file=[" + filePath + "]");
  36.                                 fileCounter.add();
  37.                             });
  38.                     } catch (IOException e) {
  39.                         throw new RuntimeException(e);
  40.                     }
  41.                 });
  42.            
  43.             LOG.info("Found " + fileCounter.get() + " files with match");
  44.             long end = System.nanoTime();
  45.             LOG.info("Time: " + (end - start)/1000000 +  " ms");
  46.         } catch (IOException e) {
  47.             throw new RuntimeException(e);
  48.         };
  49.        
  50.         LOG.info("*****************END*****************");
  51.     }
  52.    
  53.     private static final class Counter {
  54.         int counter = 0;
  55.        
  56.         public void add() {
  57.             counter++;
  58.         }
  59.        
  60.         public int get() {
  61.             return counter;
  62.         }
  63.     }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement