Ever wondered about how to import only lines from a text file you are interested in, meaning how to filter lines from a text file?

If so, the following might give you a hint for a solution.

In summary it uses regular expressions to filter only lines from text files matching a provided pattern.

A test file called "MIXED BAG COLLECTION" can be downloaded from the following link. I love movies so I used a collection of movies downloaded in a CSV file from IMDB.

images/MIXED_BAG_COLLECTION.csv

You are probably asking yourself, why would he do this just to find some good movies? I haven't seen a filter functionality in IMDB for movies by rating and I had fun coding it. 

Now I can filter in a list of movies for all movies matching a certain rating using following simple regular expression. 

"[8-9]\\.."

Source

package examples.text;

 

import java.io.File;

import java.io.FileNotFoundException;

import java.util.ArrayList;

import java.util.Scanner;

import java.util.regex.Matcher;

import java.util.regex.Pattern;

 

public class TextExample {

 

public static ArrayList<String> findLinesMatchingPattern(String filePath,

String regEx) {

ArrayList<String> ret = null;

Pattern pattern = null;

Matcher myMatch = null;

String aLine;

/*

* Try referencing a text file under given path. 

*/

File myFile = new File(filePath);

try {

/*

* Setup a scanner using UTF-8 as character set.

* UTF-8 is the internet standard, so it should fit most files,

* but with character sets you'll run into trouble sooner or later :0).

*/

Scanner myScan = new Scanner(myFile, "UTF-8");

/*

* Compile the regular expression in order to get strings from file

* matching a pattern.

*/

pattern = Pattern.compile(regEx);

/*

* Prepare an ArrayList as result.

*/

ret = new ArrayList<String>();

/*

* Walk through all lines of a file.

*/

while (myScan.hasNextLine()) {

aLine = myScan.nextLine();

/*

* Test, if line matches your pattern.

* If yes, add it to your result.

*/

myMatch = pattern.matcher(aLine);

if (myMatch.find()) {

ret.add(aLine);

}

}

} catch (FileNotFoundException e) {

ret = null;

}

 

return ret;

}

 

public static void main(String[] bla) {

ArrayList<String> result;

 

/*

* Search sample text file for all 'Hitchcock' movies,

* where Hitchcock is our sample regular expression / pattern.

* You can of course use real regular expressions.

*/

result = findLinesMatchingPattern(

"/users/somePath/text/MIXED_BAG_COLLECTION.csv",

"Hitchcock");

if (result != null

for (int i = 0; i < result.size(); i++) {

System.out.println(result.get(i));

}

else

System.out.println("File not found.");

 

/* Always leave the restroom with clean hands. */

System.exit(0);

}