Skip to Content
Java™ Phrasebook
book

Java™ Phrasebook

by Timothy Fisher
November 2006
Intermediate to advanced
224 pages
3h 29m
English
Sams
Content preview from Java™ Phrasebook

Printing Lines Containing a Pattern

String pattern = "^a";
											Pattern regPat = Pattern.compile(pattern);
											Matcher matcher = regPat.matcher("");
											BufferedReader reader =
											new BufferedReader(new FileReader("file.txt"));
											String line;
											while ((line = reader.readLine()) != null) {
											matcher.reset(line);
											if (matcher.find()) {
											System.out.println(line);
											}
											}

This phrase demonstrates how we might search through a file to find all the lines that contain a given pattern. Here we use the BufferedReader class to read lines from a text file. We attempt to match each line against our pattern using the find() method of the Matcher class. The find() method will return true if the pattern is found within the line passed as its parameter. We print all the lines that match ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Ruby Phrasebook

Ruby Phrasebook

Jason Clinton
Wicked Cool Java

Wicked Cool Java

Brian D. Eubanks
Just Java™ 2

Just Java™ 2

Peter van der Linden
What Employees Want Most in Uncertain Times

What Employees Want Most in Uncertain Times

Kristine W. Powers, Jessica B.B. Diaz

Publisher Resources

ISBN: 0768668255Purchase book