Here you can find the source of findPattern(File file, Charset charset, String patternString)
Parameter | Description |
---|---|
file | the file in which the pattern is searched |
charset | the file charset |
patternString | the string pattern to search |
public static boolean findPattern(File file, Charset charset, String patternString)
//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; } } }