简体   繁体   English

从Java中的文本文件中读取特定行

[英]Reading a specific line from a text file in Java

Is there any method to read a specific line from a text file ? 有没有方法从文本文件中读取特定的行? In the API or Apache Commons. 在API或Apache Commons中。 Something like : 就像是 :

String readLine(File file, int lineNumber)

I agree it's trivial to implement, but it's not very efficient specially if the file is very big. 我同意它的实现是微不足道的,但如果文件非常大,它的效率并不高。

String line = FileUtils.readLines(file).get(lineNumber);

would do, but it still has the efficiency problem. 会这样做,但它仍然存在效率问题。

Alternatively, you can use: 或者,您可以使用:

 LineIterator it = IOUtils.lineIterator(
       new BufferedReader(new FileReader("file.txt")));
 for (int lineNumber = 0; it.hasNext(); lineNumber++) {
    String line = (String) it.next();
    if (lineNumber == expectedLineNumber) {
        return line;
    }
 }

This will be slightly more efficient due to the buffer. 由于缓冲区,这将稍微更有效。

Take a look at Scanner.skip(..) and attempt skipping whole lines (with regex). 看一下Scanner.skip(..)并尝试跳过整行(使用正则表达式)。 I can't tell if it will be more efficient - benchmark it. 我不知道它是否会更有效 - 基准它。

PS with efficiency I mean memory efficiency PS有效率我的意思是内存效率

Not that I'm aware of. 不是我知道的。

Be aware that there's no particular indexing on files as to where the line starts, so any utility method would be exactly as efficient as: 请注意,对于行开始的位置,文件没有特定的索引,因此任何实用程序方法都可以完全有效:

BufferedReader r = new BufferedReader(new FileReader(file));
for (int i = 0; i < lineNumber - 1; i++)
{
   r.readLine();
}
return r.readLine();

(with appropriate error-handling and resource-closing logic, of course). (当然,有适当的错误处理和资源关闭逻辑)。

If the lines you were reading were all the same length, then a calculation might be useful. 如果你读的线都是相同的长度,然后计算可能是有用的。

But in the situation when the lines are different lengths, I don't think there's an alternative to reading them one at a time until the line count is correct. 但是在线条长度不同的情况下,我认为没有其他方法可以一次读取它们,直到线数正确为止。

Unfortunately, unless you can guarantee that every line in the file is the exact same length, you're going to have to read through the whole file, or at least up to the line you're after. 不幸的是,除非你能保证文件中的每一行都是完全相同的长度,否则你将不得不通读整个文件,或者至少要读到你所追踪的那一行。

The only way you can count the lines is to look for the new line characters in the file, and this means you're going to have to read each byte. 计算行数的唯一方法是在文件中查找新行字符,这意味着您将不得不读取每个字节。

It will be possible to optimise your code to make it neat and readable, but underneath you'll always be reading the whole file. 可以优化您的代码以使其整洁可读,但在下面您将始终阅读整个文件。

If you're going to reading the same file over and over again you could parse the file and create an index storing the offsets of certain line numbers, for example the byte count of where lines 100, 200 and so on are. 如果你要一遍又一遍地读取同一个文件,你可以解析文件并创建一个存储某些行号偏移量的索引,例如行100,200等的字节数。

Because files are byte and not line orientated - any general solutions complexity will be O(n) at best with n being the files size in bytes. 因为文件是字节而不是面向行 - 任何通用的解决方案复杂度最多都是O(n),n是文件大小(以字节为单位)。 You have to scan the whole file and count the line delimiters until you know which part of the file you want to read. 您必须扫描整个文件并计算行分隔符,直到您知道要读取的文件的哪个部分。

guava has something similar: 番石榴有类似的东西:

List<String> Files.readLines(File file, Charset charset);

So you can do 所以你可以做到

String line = Files.readLines(file, Charsets.UTF_8).get(lineNumber);

Using File Utils: 使用File Utils:

File fileFeatures = new File(
                "Homework1AdditionalFiles/jEdit4.3/jEdit4.3ListOfFeatureIDs.txt");
String line = (String) FileUtils.readLines(fileFeatures).get(lineNumber);

If you are going to work with the same file in the same way (looking for a text at certain line) you can index your file. 如果要以相同的方式使用同一文件(在特定行查找文本),则可以索引文件。 Line number -> offset. 行号 - >偏移量。

According to this answer , Java 8 enables us to extract specific lines from a file. 根据这个答案 ,Java 8使我们能够从文件中提取特定的行。 Examples are provided in that answer. 该答案中提供了示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM