Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.IOException;
- import java.nio.charset.Charset;
- import java.nio.file.Files;
- import java.nio.file.Paths;
- import java.util.stream.Stream;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
- import org.springframework.context.annotation.Configuration;
- @Configuration
- @EnableAutoConfiguration
- public class StringMatching {
- private static final Logger LOG = LoggerFactory.getLogger(StringMatching.class);
- public void match(String match, String dirPath, String fileNamePrefix) {
- LOG.info("Match=" + match);
- LOG.info("DirPath=" + dirPath);
- LOG.info("FileNamePrefix=" + fileNamePrefix);
- LOG.info("*****************START*****************");
- LOG.info("Threads: " + Runtime.getRuntime().availableProcessors());
- try {
- long start = System.nanoTime();
- final Counter fileCounter = new Counter();
- Files.newDirectoryStream(Paths.get(dirPath), p -> p.getFileName().toString().startsWith(fileNamePrefix) && !Files.isDirectory(p)).
- forEach(filePath -> {
- LOG.info("Searching in " + filePath + " ...");
- try (Stream<String> lines = Files.lines(filePath, Charset.defaultCharset())) {
- lines.filter(line -> line.contains(match)).findAny().
- ifPresent(matchingLine -> {
- LOG.info("Found match in file=[" + filePath + "]");
- fileCounter.add();
- });
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
- });
- LOG.info("Found " + fileCounter.get() + " files with match");
- long end = System.nanoTime();
- LOG.info("Time: " + (end - start)/1000000 + " ms");
- } catch (IOException e) {
- throw new RuntimeException(e);
- };
- LOG.info("*****************END*****************");
- }
- private static final class Counter {
- int counter = 0;
- public void add() {
- counter++;
- }
- public int get() {
- return counter;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement