Zerhinne
Zerhinne

Reputation: 2057

Append a specific line BufferedReader java

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

Answers (2)

sgibly
sgibly

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

Ryan Stewart
Ryan Stewart

Reputation: 128919

  1. Create the BufferedReader.
  2. Use readLine() to get a line at a time.
  3. Check if it's the line you're looking for, maybe using contains(). If so, store it in a String variable.
  4. Close the reader.
  5. Print the String.

Upvotes: 4

Related Questions