public static ArrayList < String > getFilteredLinesFromFile ( final String fileName , final String filter , final String removeBeforeToken , final String removeAfterToken ) throws IOException { final ArrayList < String > filteredLines = new ArrayList <> ( ) ; try ( final BufferedReader in = new BufferedReader ( new InputStreamReader ( new FileInputStream ( fileName ) , StandardCharsets . UTF_8 ) ) ) { String line = "" ; while ( ( line = in . readLine ( ) ) != null ) { if ( line . trim ( ) . length ( ) == 0 ) { continue ; } if ( line . contains ( filter ) ) { String trimedLine ; if ( removeBeforeToken != null ) { final String [ ] p = line . split ( removeBeforeToken ) ; if ( p . length > 1 ) { trimedLine = p [ p . length - 1 ] ; } else { trimedLine = line . trim ( ) ; } } else { trimedLine = line . trim ( ) ; } if ( removeAfterToken != null ) { final String [ ] p = trimedLine . split ( removeAfterToken ) ; if ( p . length > 1 ) { trimedLine = p [ 0 ] ; } } filteredLines . add ( trimedLine ) ; } } } return filteredLines ; }
Get lines from a given file with a specified filter trim the line by removing strings before removeBeforeToken and after removeAfterToken .