随机访问一行文本文件并从头到尾读取行 [英] Random access to a line of Text File and read lines from end to first line

查看:115
本文介绍了随机访问一行文本文件并从头到尾读取行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我有一个文本文件(扩展名为.txt)。这个文件有很多行(超过30000行)

读取它的行是我使用的StreamReader。



1-(C#)中是否有任何命令用于读取文本文件(而不是StreamReader)?

2-我怎么能拥有随机访问一条线?例如,访问第1000行?

3-如何从头到尾读取行?

例如:



for(int i =文件行数; i> 0; i--)

{

sr.ReadLine();

}



非常感谢

Hi I have a text file (by extension .txt) . this file has very lines (more than 30000 lines)
For read it’s lines I use of "StreamReader".

1- Is there any command in (C#) for read text file (instead of StreamReader)?
2- How can I have random access to a line? For example access to line 1000?
3- How can I read lines from end to first?
for example:

for (int i = line count of file; i > 0; i--)
{
sr.ReadLine();
}

Thanks very much

推荐答案

您无法真正随机访问文本文件中的各个行,因为它们的长度不同。



实际上,您可以创建一些可以支持这种随机访问的数据结构,但是在您至少读取整个文件一次之后就无法完成。你的问题#1毫无意义:没有C#中的命令这样的东西。问题更为根本,无论有人编写什么代码,它都存在。这些行不是某个对象的集合:除了一些统一的文本之外什么都没有,并且这些行简单地用一些行分隔符分隔。请参阅:

http://en.wikipedia.org/wiki/Newline [< a href =http://en.wikipedia.org/wiki/Newlinetarget =_ blanktitle =New Window> ^ ]。



由于线条长度不同,每条线的起始位置不均匀分布。你只有在读完整个文件时才能找到它们。



这是你应该做的:如果文件很小,你应该把它全部读入内存,作为一系列线条。我想你明白了。他们可以随意访问行。



如果文件很大,你仍然需要全部阅读,但你只需要记录下表文件中的行位置。稍后,您将能够找到咨询此表的每一行的正确位置。如果文件更大,你甚至无法将这个表放在内存中。然后你可以写另一个文件,一个用来存储这个表的临时文件。由于此文件的记录大小相同,因此您不会遇到表中记录分布不均匀的问题。因此,当创建表时(整个技术称为索引),您将具有O(1)的时间复杂度来访问单个行。请参阅:

http://en.wikipedia.org/wiki/Big_O_notation [< a href =http://en.wikipedia.org/wiki/Big_O_notationtarget =_ blanktitle =New Window> ^ ],

http://en.wikipedia.org/wiki/Time_complexity [ ^ ],

http://en.wikipedia.org/wiki/Worst-case_complexity [ ^ ],

http://en.wikipedia.org/wiki / Computational_complexity_theory [ ^ ]。



-SA
You cannot really have random access to individual lines in a text file, simply because they have different lengths.

Actually, you can create some data structure which could support such random access, but it cannot be done no sooner that you read the whole file at least once. And your question #1 makes no sense: there is no such thing as "command in C#". The problem is more fundamental, it exists no matter what code someone would write. The lines are not a collection of some object: there is nothing but some uniform text, and the lines are simply separated by some line separator. Please see:
http://en.wikipedia.org/wiki/Newline[^].

As lines have different length, the starting positions of each line are distributed non-uniformly. You can find them out only when you read the whole file.

Here is what you should do: if the file is small, you should read it all into memory, as a collection of lines. I think you understand it. Them you would have the random access to lines.

If the file is big, you still need to read it all, but you would need to record only the table of line positions in the file. Later on, you would be able to get to right position for each line consulting this table. If the file is even bigger, your cannot fit even this table in memory. Then you can write yet another file, a temporary file used to store this table. As the record of this file are of the same size, you won't have the problem with non-uniform distribution of the records in the table. So, when the table is created (the whole technique is called "indexing"), you would have the time complexity of O(1) for access to an individual line. Please see:
http://en.wikipedia.org/wiki/Big_O_notation[^],
http://en.wikipedia.org/wiki/Time_complexity[^],
http://en.wikipedia.org/wiki/Worst-case_complexity[^],
http://en.wikipedia.org/wiki/Computational_complexity_theory[^].

—SA


这篇关于随机访问一行文本文件并从头到尾读取行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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