如何使用Java文件中的特定行号读取特定行?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2312756/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to read a specific line using the specific line number from a file in Java?
提问by trinity
In Java, is there any method to read a particular line from a file? For example, read line 32 or any other line number.
在 Java 中,是否有任何方法可以从文件中读取特定行?例如,读取第 32 行或任何其他行号。
采纳答案by Joachim Sauer
Unless you have previous knowledge about the lines in the file, there's no way to directly access the 32nd line without reading the 31 previous lines.
除非您事先了解文件中的行,否则无法在不阅读前 31 行的情况下直接访问第 32 行。
That's true for all languages and all modern file systems.
对于所有语言和所有现代文件系统都是如此。
So effectively you'll simply read lines until you've found the 32nd one.
如此有效,您只需阅读行,直到找到第 32 行。
回答by Uri
If you are talking about a text file, then there is really no way to do this without reading all the lines that precede it - After all, lines are determined by the presence of a newline, so it has to be read.
如果你在谈论一个文本文件,那么如果不阅读它之前的所有行,真的没有办法做到这一点 - 毕竟,行是由换行符的存在决定的,所以必须阅读它。
Use a stream that supports readline, and just read the first X-1 lines and dump the results, then process the next one.
使用支持 readline 的流,只需读取前 X-1 行并转储结果,然后处理下一行。
回答by Chris Thompson
Not that I know of, but what you could do is loop through the first 31 lines doing nothing using the readline() function of BufferedReader
不是我所知道的,但是您可以做的是使用 BufferedReader 的 readline() 函数循环遍历前 31 行而不执行任何操作
FileInputStream fs= new FileInputStream("someFile.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(fs));
for(int i = 0; i < 31; ++i)
br.readLine();
String lineIWant = br.readLine();
回答by b.roth
No, unless in that file format the line lengths are pre-determined (e.g. all lines with a fixed length), you'll have to iterate line by line to count them.
不,除非在该文件格式中,行长度是预先确定的(例如所有具有固定长度的行),否则您必须逐行迭代以计算它们。
回答by Charlie Collins
Joachim is right on, of course, and an alternate implementation to Chris' (for small files only because it loads the entire file) might be to use commons-io from Apache (though arguably you might not want to introduce a new dependency just for this, if you find it useful for other stuff too though, it could make sense).
当然,Joachim 是正确的,而 Chris 的替代实现(仅适用于小文件,因为它加载整个文件)可能是使用来自 Apache 的 commons-io(尽管可以说您可能不想引入新的依赖项只是为了这个,如果你发现它对其他东西也有用,它可能是有道理的)。
For example:
例如:
String line32 = (String) FileUtils.readLines(file).get(31);
http://commons.apache.org/io/api-release/org/apache/commons/io/FileUtils.html#readLines(java.io.File, java.lang.String)
http://commons.apache.org/io/api-release/org/apache/commons/io/FileUtils.html#readLines(java.io.File, java.lang.String)
回答by Raghavendra
You can use LineNumberReader instead of BufferedReader. Go through the api. You can find setLineNumber and getLineNumber methods.
您可以使用 LineNumberReader 而不是 BufferedReader。通过api。您可以找到 setLineNumber 和 getLineNumber 方法。
回答by Ankur Shanbhag
You can also take a look at LineNumberReader, subclass of BufferedReader. Along with the readline method, it also has setter/getter methods to access line number. Very useful to keep track of the number of lines read, while reading data from file.
您还可以查看 LineNumberReader,BufferedReader 的子类。除了 readline 方法,它还具有 setter/getter 方法来访问行号。在从文件读取数据时跟踪读取的行数非常有用。
回答by jramoyo
You may try indexed-file-reader(Apache License 2.0). The class IndexedFileReader has a method called readLines(int from, int to)which returns a SortedMap whose key is the line number and the value is the line that was read.
您可以尝试indexed-file-reader(Apache License 2.0)。IndexedFileReader 类有一个名为readLines(int from, int to) 的方法,它返回一个 SortedMap,其键是行号,值是读取的行。
Example:
例子:
File file = new File("src/test/resources/file.txt");
reader = new IndexedFileReader(file);
lines = reader.readLines(6, 10);
assertNotNull("Null result.", lines);
assertEquals("Incorrect length.", 5, lines.size());
assertTrue("Incorrect value.", lines.get(6).startsWith("[6]"));
assertTrue("Incorrect value.", lines.get(7).startsWith("[7]"));
assertTrue("Incorrect value.", lines.get(8).startsWith("[8]"));
assertTrue("Incorrect value.", lines.get(9).startsWith("[9]"));
assertTrue("Incorrect value.", lines.get(10).startsWith("[10]"));
The above example reads a text file composed of 50 lines in the following format:
上面的例子读取一个由 50 行组成的文本文件,格式如下:
[1] The quick brown fox jumped over the lazy dog ODD
[2] The quick brown fox jumped over the lazy dog EVEN
Disclamer: I wrote this library
免责声明:我写了这个库
回答by user3526115
They are all wrong I just wrote this in about 10 seconds. With this I managed to just call the object.getQuestion("linenumber") in the main method to return whatever line I want.
他们都错了,我只是在大约 10 秒内写了这个。有了这个,我设法在 main 方法中调用 object.getQuestion("linenumber") 来返回我想要的任何行。
public class Questions {
File file = new File("Question2Files/triviagame1.txt");
public Questions() {
}
public String getQuestion(int numLine) throws IOException {
BufferedReader br = new BufferedReader(new FileReader(file));
String line = "";
for(int i = 0; i < numLine; i++) {
line = br.readLine();
}
return line; }}
回答by aioobe
For small files:
对于小文件:
String line32 = Files.readAllLines(Paths.get("file.txt")).get(32)
For large files:
对于大文件:
try (Stream<String> lines = Files.lines(Paths.get("file.txt"))) {
line32 = lines.skip(31).findFirst().get();
}