如何读取文本文件中的指定行

如何读取文本文件中的指定行

本文介绍了如何读取文本文件中的指定行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定一个文本文件,我将如何读取任意行而文件中没有其他内容?

Given a text file, how would I go about reading an arbitrary line and nothing else in the file?

比如说,我有一个文件 test.txt.我将如何阅读文件中的第 15 行?

Say, I have a file test.txt. How would I go about reading line number 15 in the file?

我所看到的只是将整个文本文件存储为一个字符串数组,然后使用行号的值作为要从数组中使用的字符串的编号……但有一些复杂情况:文本文件非常庞大,我正在编写的应用程序的机器并不是一流的系统.速度不是首要任务,但绝对是一个主要问题.

All I've seen is stuff involving storing the entire text file as a String array and then using the value of the line number as the number of the String to use from the array... but there are some complications: The text file is enormously huge and the machine that the application I'm coding isn't exactly a top-notch system. Speed isn't the top priority, but it is definitely a major issue.

有什么方法可以读取文本文件的特定行并将结果存储为字符串?

Are there any ways to ONLY read a specific line of a text file and store the result as a string?

感谢您的回复:该文件是 KINDA 结构的.它有 25 行信息,然后是 X 行数字,但前 25 行的第 17 行的值为 X.

Thanks for your responses:The file is KINDA structured. It's got 25 lines of info and then X lines of numbers but line 17 of the first 25 has the value of X.

但是,有 1 个空行,它作为文件中的第二条记录重复出现,并且 X 可以为每条记录设置不同的值.

But then, there's 1 blank line and it repeats itself all over as a second record in the file and X can have a different value for each record.

我想要做的是读取并存储前 25 行作为独立值,然后将下一个 X(通常约为 250)行存储为数组.然后我将它存储在一个 SQL 数据库中并用 NEXT 记录重复,直到我到达 Yth 记录(文件中的记录数在第 3 行)

What I want to do is read and store the first 25 lines as independent values and then store the next X (usually around 250) lines as an array. Then I'm going to store it in an SQL database and repeat with the NEXT record until I reach the Yth record (the number of records in the file is in line 3)

编辑 2:好的,我想我已经根据大家的回答找到了一个解决方案.

EDIT 2:Alright, I think I've gotten to a solution based on a combination of your alls' responses.

我将读取前 25 行并将其存储为数组.我会将数组的相关内容复制到局部变量,然后删除前 25 行.然后,我可以使用该信息将下一个 X 行(数组中第 13 项的值)存储为一个数组,对其进行序列化,将其存储在数据库中,然后删除我刚刚读取的行.

I'm going to read the first 25 lines and store it as an array. I'll copy the pertinent contents of the array to local variables then I'll delete the first 25 lines. Then, I can use the info to store the next X lines (the value of item 13 in the array) as an array, serialize it, store it in a database then delete the lines that I just read.

然后我可以为每个后续记录重复该过程.

I could then repeat the process for each subsequent record.

当然,这取决于我所做的一个假设,说实话,我不确定这是真的.是否可以从 C# 中的文本文件中删除前 n 行而不必读取整个内容并在没有前 n 行的情况下重新编写它?>

Of course, this relies on one assumption I'm making, which to be honest, I'm not sure is true. Is it possible to delete the first n lines from a text file from within C# without having to read the entire thing and re-write it without the first n lines?

推荐答案

.NET 4.0 编辑

从 .NET 4.0 开始,可以直接访问文件的单行.例如,要访问第 15 行:

Since .NET 4.0, it is possible to access a single line of a file directly. For instance, to access line 15:

string line = File.ReadLines(FileName).Skip(14).Take(1).First();

这将只返回所需的行

由于您无法预测文件中第 i 行的位置(您能吗?),因此您也必须阅读之前的所有行.如果行号很小,这可能比 ReadAllLines 方法更有效.

Since you can't predict the location (can you?) of the i-th line in the file, you'll have to read all previous lines too. If the line number is small, this can be more efficient than the ReadAllLines method.

string GetLine(string fileName, int line)
{
   using (var sr = new StreamReader(fileName)) {
       for (int i = 1; i < line; i++)
          sr.ReadLine();
       return sr.ReadLine();
   }
}

这篇关于如何读取文本文件中的指定行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 04:29