Java Text File Read Line readLinesWithPattern(File file, Pattern pattern)

Here you can find the source of readLinesWithPattern(File file, Pattern pattern)

Description

read Lines With Pattern

License

Open Source License

Declaration

public static ArrayList<String> readLinesWithPattern(File file, Pattern pattern) 

Method Source Code


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

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.regex.Pattern;

public class Main {
    public static ArrayList<String> readLinesWithPattern(File file, Pattern pattern) {
        ArrayList<String> strs = new ArrayList<>();
        try {/*from w  ww  . jav a  2 s .c  o  m*/
            Scanner sc = new Scanner(file);
            while (sc.hasNextLine()) {
                Scanner lineSc = new Scanner(sc.nextLine());
                String str = lineSc.findInLine(pattern);
                if (str != null) {
                    strs.add(str.trim());
                }
            }
        } catch (FileNotFoundException ex) {
            // Ignore
        }
        return strs;
    }
}

Related

  1. readLinesFileSimple(final File inFile, final int prefixMode, final String prefix)
  2. readLinesFromCommand(String command[], List buffer)
  3. readLinesNoComments(String fileName)
  4. readLinesRaw(final File inFile)
  5. readLinesTrimmedNoComment(final String fileName, final String commentString)