Java File Read by Charset findPattern(File file, Charset charset, String patternString)

Here you can find the source of findPattern(File file, Charset charset, String patternString)

Description

Search for a string pattern in a specific file

License

Open Source License

Parameter

Parameter Description
file the file in which the pattern is searched
charset the file charset
patternString the string pattern to search

Return

true if pattern found in file, false if not

Declaration

public static boolean findPattern(File file, Charset charset, String patternString) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import com.google.common.io.Files;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    /**//from  ww w.  j  a  v a  2  s  . co m
     * Search for a string pattern in a specific file
     * @param file the file in which the pattern is searched
     * @param charset the file charset
     * @param patternString the string pattern to search
     * @return true if pattern found in file, false if not
     */
    public static boolean findPattern(File file, Charset charset, String patternString) {
        List<String> lines;

        try {
            lines = Files.readLines(file, charset);

        } catch (IOException e) {
            String fileName = file.getName();
            throw new IllegalStateException("Unable to execute rule \"S1451\" for file " + fileName, e);
        }

        if (lines == null) {
            return false;
        } else {
            boolean result = false;
            Pattern pattern = Pattern.compile(patternString);
            for (String line : lines) {
                Matcher matcher = pattern.matcher(line);
                result |= matcher.find();
            }
            return result;
        }
    }
}

Related

  1. detectCharset(File f, String[] charsets)
  2. doTranseFileCharset(File srcFile, File destFile, String srcCharsetName, String destCharsetName)
  3. fileContent(File file, Charset charset)
  4. fileToString(final File f, final Charset c)
  5. fileToString(final Path path, final Charset charset)
  6. fixture(String filename, Charset charset)
  7. getContent(File file, String charsetName)
  8. getEOL(File file, Charset charset)
  9. getFileContent(File file, String charsetName)