且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

阅读在特定的行文本文件

更新时间:1970-01-01 07:58:48

下面是一个迭代的文件两次溶液(第一次计算行,接下来的时间来选择线)。这样做的好处是,你不需要在内存中创建3000字符串数组。但是,如上所述,这将可能的比较慢。为什么可能? - 因为 File.ReadAllLines 里面创建一个字符串列表,并同时与3000项填充它该列表将被调整多次。 (初始容量将 4 。当内阵列完全填充,则增加了一倍大小的新数组将被创建,所有的字符串将在这里复制)。

字符串> $ C>

File.ReadLines 方法,它返回的IEnumerable&LT> $ 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.