【发布时间】:2018-03-16 21:25:53
【问题描述】:
不知道有没有可能:
我想知道在 someFile 中找到 someWord 的数字行。
try {
CharsetDecoder dec = StandardCharsets.UTF_8.newDecoder()
.onMalformedInput(CodingErrorAction.IGNORE);
try (Reader r = Channels.newReader(FileChannel.open("path"), dec, -1);
BufferedReader br = new BufferedReader(r)) {
br.lines().filter(line -> line.contains("SomeWord"))
.forEach(line -> System.out.println("location:" + line.????)); //Location where line has the "SomeWord"
}
} catch (IOException |java.io.UncheckedIOException ex) {
Logger.getLogger(RecursiveFolderAndFiles.class.getName())
.log(Level.SEVERE, null, ex);
}
我该怎么做?
【问题讨论】:
标签: foreach java-8 location java-stream lines