我如何读取一个文本文件中的指定行? [英] How do I read a specified line in a text file?

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

问题描述

给定一个文本文件,我将如何去读取文件中的独断专行,没有别的?

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?

我所见过的东西是涉及存储整个文本文件作为一个String数组,然后使用该行号作为字符串从数组使用数量的价值......但也有一些并发症:本文本文件是极其巨大的,而且我编码申请不完全是一流的系统设备。速度不是首要任务,但它绝对是一个重大问题。

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?

您的答复感谢:
该文件是有点儿结构。它有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数据库,并与下一个记录重复,直到我到达的的个记录(记录文件中的数字是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();

这将只返回所需的行

既然你不能predict的位置(可以吗?)在文件中第i行,你必须阅读所有previous行了。如果行数是小的,这可以比 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();
   }
}

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆