I need to process a large file, around 400K lines and 200 M. But sometimes I have to process from bottom up. How can I use iterator (yield return) here? Basically I don't like to load everything in memory. I know it is more efficient to use iterator in .NET.
相关问题
- Sorting 3 numbers without branching [closed]
- Graphics.DrawImage() - Throws out of memory except
- Generic Generics in Managed C++
- Why am I getting UnauthorizedAccessException on th
- 求获取指定qq 资料的方法
To create a file iterator you can do this:
EDIT:
This is my fixed version of a fixed-width reverse file reader:
Reading text files backwards is really tricky unless you're using a fixed-size encoding (e.g. ASCII). When you've got variable-size encoding (such as UTF-8) you will keep having to check whether you're in the middle of a character or not when you fetch data.
There's nothing built into the framework, and I suspect you'd have to do separate hard coding for each variable-width encoding.
EDIT: This has been somewhat tested - but that's not to say it doesn't still have some subtle bugs around. It uses StreamUtil from MiscUtil, but I've included just the necessary (new) method from there at the bottom. Oh, and it needs refactoring - there's one pretty hefty method, as you'll see:
Feedback very welcome. This was fun :)
You could use File.ReadLines to get lines iterator
EDIT:
After reading applejacks01's comment, I run some tests and it does look like
.Reverse()
actually loads whole file.I used
File.ReadLines()
to print first line of a 40MB file - memory usage of console app was 5MB. Then, usedFile.ReadLines().Reverse()
to print last line of same file - memory usage was 95MB.I wanted to do the similar thing. Here is my code. This class will create temporary files containing chunks of the big file. This will avoid memory bloating. User can specify whether s/he wants the file reversed. Accordingly it will return the content in reverse manner.
This class can also be used to write big data in a single file without bloating memory.
Please provide feedback.
This service can be used as follows -
I put the file into a list line by line, then used List.Reverse();
...
I know this post is very old but as I couldn't find how to use the most voted solution, I finally found this: here is the best answer I found with a low memory cost in VB and C#
http://www.blakepell.com/2010-11-29-backward-file-reader-vb-csharp-source
Hope, I'll help others with that because it tooks me hours to finally find this post!
[Edit]
Here is the c# code :