阅读在特定的行文本文件 [英] Read text file at specific line
问题描述
我已经拥有超过3000行的文本文件。我使用发现的行数
的String [] =行File.ReadAllLines(mypath中);
VAR lineCount = lines.Length;
然后我生成一个随机数
随机兰特=新的随机();
变种lineToRead = rand.Next(1,lineCount);
现在我需要阅读由随机数产生的具体线路。我能做到这一点使用
字符串requiredLine =行[lineToRead]
由于我的文件特别大,我不认为建立这样一个大数组是有效的。是否有更有效或更简单的方法来做到这一点?
下面是一个迭代的文件两次溶液(第一次计算行,接下来的时间来选择线)。这样做的好处是,你不需要在内存中创建3000字符串数组。但是,如上所述,这将<青霉>可能的比较慢。为什么可能? - 因为 File.ReadAllLines
里面创建一个字符串列表,并同时与3000项填充它该列表将被调整多次。 (初始容量将 4
。当内阵列完全填充,则增加了一倍大小的新数组将被创建,所有的字符串将在这里复制)。
File.ReadLines 方法,它返回的IEnumerable<> $ C>用线条和线条跳过你不需要:
的IEnumerable<串GT;线= File.ReadLines(mypath中);
变种lineToRead = rand.Next(1,lines.Count());
无功行= lines.Skip(lineToRead - 1)。首先();
顺便说一句,在内部 File.ReadLines
使用 SteamReader
这逐行读取文件中的行。
I have a text file which has more than 3000 lines. I am finding the number of lines using
string[] lines = File.ReadAllLines(myPath);
var lineCount = lines.Length;
Then I am generating a random number
Random rand = new Random();
var lineToRead = rand.Next(1, lineCount);
Now I need to read the specific line that is generated by random number. I can do this using
string requiredLine = lines[lineToRead];
Because my file is big I don't think creating such a big array is efficient. Is there a more efficient or easier way to do this?
Here is a solution which iterates the file twice (first time to count lines, next time to select line). The benefit is that you don't need to create an array of 3000 strings in memory. But, as mentioned above, it will possibly be slower. Why possibly? - because File.ReadAllLines
creates a list of strings inside and that list will be resized many times while filling it with 3000 items. (Initial capacity will be 4
. When the inner array is completely filled, then the new array of doubled size will be created and all strings will be copied there).
So, the solution uses File.ReadLines
method which returns IEnumerable<string>
with lines and skip lines you don't need:
IEnumerable<string> lines = File.ReadLines(myPath);
var lineToRead = rand.Next(1, lines.Count());
var line = lines.Skip(lineToRead - 1).First();
BTW, internally File.ReadLines
uses SteamReader
which reads file line by line.
这篇关于阅读在特定的行文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!