我有以下代码,它读取文件并搜索以主题开头的行并打印出该行。但我只想读取包含“主题”的第一行,但目前它获取以“主题”开头的所有行,如何配置以使其搜索第一个“主题”并打印该行?
br = new BufferedReader(new FileReader("FilePath"));
StringBuilder sb = new StringBuilder();
String line = "";
while ((line = br.readLine()) != null) {
if (line.startsWith("Subject ")) {
System.out.println(line);
}
}
最佳答案
使用中断;声明,
br = new BufferedReader(new FileReader("FilePath"));
StringBuilder sb = new StringBuilder();
String line = "";
while ((line = br.readLine()) != null) {
if (line.startsWith("Subject ")) {
System.out.println(line);
break;
}
}
关于Java读取文本文件并搜索一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21542881/