Is there an easy way to programmatically determine the number of lines within a text file?
相关问题
- Sorting 3 numbers without branching [closed]
- Graphics.DrawImage() - Throws out of memory except
- Why am I getting UnauthorizedAccessException on th
- 求获取指定qq 资料的方法
- How to know full paths to DLL's from .csproj f
You could quickly read it in, and increment a counter, just use a loop to increment, doing nothing with the text.
Reading a file in and by itself takes some time, garbage collecting the result is another problem as you read the whole file just to count the newline character(s),
At some point, someone is going to have to read the characters in the file, regardless if this the framework or if it is your code. This means you have to open the file and read it into memory if the file is large this is going to potentially be a problem as the memory needs to be garbage collected.
Nima Ara made a nice analysis that you might take into consideration
Here is the solution proposed, as it reads 4 characters at a time, counts the line feed character and re-uses the same memory address again for the next character comparison.
Above you can see that a line is read one character at a time as well by the underlying framework as you need to read all characters to see the line feed.
If you profile it as done bay Nima you would see that this is a rather fast and efficient way of doing this.
The easiest:
You can launch the "wc.exe" executable (comes with UnixUtils and does not need installation) run as an external process. It supports different line count methods (like unix vs mac vs windows).
Seriously belated edit: If you're using .NET 4.0 or later
The
File
class has a newReadLines
method which lazily enumerates lines rather than greedily reading them all into an array likeReadAllLines
. So now you can have both efficiency and conciseness with:Original Answer
If you're not too bothered about efficiency, you can simply write:
For a more efficient method you could do:
Edit: In response to questions about efficiency
The reason I said the second was more efficient was regarding memory usage, not necessarily speed. The first one loads the entire contents of the file into an array which means it must allocate at least as much memory as the size of the file. The second merely loops one line at a time so it never has to allocate more than one line's worth of memory at a time. This isn't that important for small files, but for larger files it could be an issue (if you try and find the number of lines in a 4GB file on a 32-bit system, for example, where there simply isn't enough user-mode address space to allocate an array this large).
In terms of speed I wouldn't expect there to be a lot in it. It's possible that ReadAllLines has some internal optimisations, but on the other hand it may have to allocate a massive chunk of memory. I'd guess that ReadAllLines might be faster for small files, but significantly slower for large files; though the only way to tell would be to measure it with a Stopwatch or code profiler.