All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.ngeor.yak4j.TextUtil Maven / Gradle / Ivy

There is a newer version: 0.13.1
Show newest version
package com.github.ngeor.yak4j;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Utility for text files.
 */
class TextUtil {
    /**
     * Filters the given stream.
     */
    List filter(InputStream inputStream, Pattern pattern) throws IOException {
        try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream))) {
            return filter(bufferedReader, pattern);
        }
    }

    /**
     * Filters the given file.
     */
    List filter(File file, Pattern pattern) throws IOException {
        try (BufferedReader bufferedReader = new BufferedReader(new FileReader(file))) {
            return filter(bufferedReader, pattern);
        }
    }

    /**
     * Filters the given reader.
     */
    List filter(BufferedReader bufferedReader, Pattern pattern) throws IOException {
        String line = bufferedReader.readLine();
        List result = new ArrayList<>();

        while (line != null) {
            Matcher matcher = pattern.matcher(line);
            if (matcher.find()) {
                result.add(new String[]{line, matcher.group(1)});
            }

            line = bufferedReader.readLine();
        }

        return result;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy