Reputation: 2057
I want to find a specific line in a BufferedReader which contains, for example, "Result" and store the entire line in a string variable, then print out the string. Is there any ways to do so?
Upvotes: 1
Views: 4429
Reputation: 3838
try {
String toFind = "Result";
String line = null;
StringBuilder buffer = new StringBuilder();
while ((line = reader.readLine()) != null) {
if (line.indexOf(toFind) > -1) { // can also use contains()
buffer.append(line);
buffer.append('\n');
}
}
// ... Print the buffer like that, or by calling a utility method
System.out.println(buffer);
} finally {
reader.close();// wrap in try-catch for any IOE
}
Upvotes: 1
Reputation: 128919
Upvotes: 4