使用 Java 给定起始行号和结束行号读取文件
Reading File using Java given a starting and an ending line number
我是 Java 编程新手,我无法在任何地方找到问题的答案。
如何读取文件的几行并将其存储在 String
(或字符串列表)中。
例如,从一个 1000 行的文件中,我只需要读取从第 200 行到第 400 行的 200 行。
我遇到了FileInputStream
使用它我们可以定义起始位置但我们不能定义结束位置。
您不能直接这样做。您只能通过阅读并忽略您不关心的第一行来做到这一点。
你可以用 Java 的 LineNumberReader
做到这一点。用它来读取文件,一次读取一行。继续阅读并忽略行,直到到达第 200 行,开始处理数据,一旦到达 400 就停止。
注意:在你问之前,不,LineNumberReader#setLineNumber
确实没有改变文件位置,它只是人为设置了报告的行号:
By default, line numbering begins at 0. This number increments at every line terminator as the data is read, and can be changed with a call to setLineNumber(int)
. Note however, that setLineNumber(int)
does not actually change the current position in the stream; it only changes the value that will be returned by getLineNumber()
.
另一种选择是只使用 BufferedReader
,调用 readLine()
199 次以跳到第 200 行,然后读取接下来的 200(或其他)行。但是,LineNumberReader
只是方便地为您记录行号。
第三种选择,因为 Java 8,是使用 streams API 并执行如下操作:
Files.lines(Paths.get("input.txt"))
.skip(200) // skip to line 200
.limit(200) // discard all but the 200 lines following this
.forEach(...)
正在将您的处理 Consumer
传递给 forEach()
。
无论哪种方式,相同的概念:您必须读取并丢弃文件的前 N 行,您无法绕过它。
示例LineNumberReader
:
LineNumberReader reader = new LineNumberReader(new FileReader("input.txt"));
for (String line = reader.readLine(); line != null; line = reader.readLine()) {
if (reader.getLineNumber() > 400) {
break; // we're done
} else if (reader.getLineNumber() >= 200) {
// do something with 'line'
}
}
带有 BufferedReader
的示例,不像上面的那么简单:
BufferedReader reader = new BufferedReader(new FileReader("input.txt"));
// skip to the 200th line
for (int n = 0; n < 199 && reader.readLine() != null; ++ n)
;
// process the next 201 (there's 201 lines between 200 and 400, inclusive)
String line;
for (int n = 0; n < 201 && (line = reader.readLine()) != null; ++ n) {
// do something with 'line'
}
上面已经给出了使用 Files
的示例。
你想如何在你的 for
或 while
或任何循环中组织条件和测试 EOF 等更多是个人喜好问题,这些只是任意说明性示例。
我是 Java 编程新手,我无法在任何地方找到问题的答案。
如何读取文件的几行并将其存储在 String
(或字符串列表)中。
例如,从一个 1000 行的文件中,我只需要读取从第 200 行到第 400 行的 200 行。
我遇到了FileInputStream
使用它我们可以定义起始位置但我们不能定义结束位置。
您不能直接这样做。您只能通过阅读并忽略您不关心的第一行来做到这一点。
你可以用 Java 的 LineNumberReader
做到这一点。用它来读取文件,一次读取一行。继续阅读并忽略行,直到到达第 200 行,开始处理数据,一旦到达 400 就停止。
注意:在你问之前,不,LineNumberReader#setLineNumber
确实没有改变文件位置,它只是人为设置了报告的行号:
By default, line numbering begins at 0. This number increments at every line terminator as the data is read, and can be changed with a call to
setLineNumber(int)
. Note however, thatsetLineNumber(int)
does not actually change the current position in the stream; it only changes the value that will be returned bygetLineNumber()
.
另一种选择是只使用 BufferedReader
,调用 readLine()
199 次以跳到第 200 行,然后读取接下来的 200(或其他)行。但是,LineNumberReader
只是方便地为您记录行号。
第三种选择,因为 Java 8,是使用 streams API 并执行如下操作:
Files.lines(Paths.get("input.txt"))
.skip(200) // skip to line 200
.limit(200) // discard all but the 200 lines following this
.forEach(...)
正在将您的处理 Consumer
传递给 forEach()
。
无论哪种方式,相同的概念:您必须读取并丢弃文件的前 N 行,您无法绕过它。
示例LineNumberReader
:
LineNumberReader reader = new LineNumberReader(new FileReader("input.txt"));
for (String line = reader.readLine(); line != null; line = reader.readLine()) {
if (reader.getLineNumber() > 400) {
break; // we're done
} else if (reader.getLineNumber() >= 200) {
// do something with 'line'
}
}
带有 BufferedReader
的示例,不像上面的那么简单:
BufferedReader reader = new BufferedReader(new FileReader("input.txt"));
// skip to the 200th line
for (int n = 0; n < 199 && reader.readLine() != null; ++ n)
;
// process the next 201 (there's 201 lines between 200 and 400, inclusive)
String line;
for (int n = 0; n < 201 && (line = reader.readLine()) != null; ++ n) {
// do something with 'line'
}
上面已经给出了使用 Files
的示例。
你想如何在你的 for
或 while
或任何循环中组织条件和测试 EOF 等更多是个人喜好问题,这些只是任意说明性示例。