我想在BufferedReader中找到一个特定的行,其中包含例如“Result”并将整行存储在字符串变量中,然后打印出该字符串。有没有办法这样做?
答案 0 :(得分:4)
答案 1 :(得分:1)
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
}