有没有一种简单的方法,以编程方式确定一个文本文件中的行数?
Answer 1:
Seriously belated edit: If you're using .NET 4.0 or later
The File
class has a new ReadLines
method which lazily enumerates lines rather than greedily reading them all into an array like ReadAllLines
. So now you can have both efficiency and conciseness with:
var lineCount = File.ReadLines(@"C:\file.txt").Count();
Original Answer
If you're not too bothered about efficiency, you can simply write:
var lineCount = File.ReadAllLines(@"C:\file.txt").Length;
For a more efficient method you could do:
var lineCount = 0;
using (var reader = File.OpenText(@"C:\file.txt"))
{
while (reader.ReadLine() != null)
{
lineCount++;
}
}
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.
Answer 2:
最简单的:
int lines = File.ReadAllLines("myfile").Length;
Answer 3:
这将使用较少的内存,但可能需要更长的时间
int count = 0;
string line;
TextReader reader = new StreamReader("file.txt");
while ((line = reader.ReadLine()) != null)
{
count++;
}
reader.Close();
Answer 4:
如果方便你的意思是很容易破解,但每个机会低效的代码行?
string[] lines = System.IO.File.RealAllLines($filename);
int cnt = lines.Count();
这可能是知道有多少行的最快方式。
你也可以做(取决于如果你是在缓冲的话)
#for large files
while (...reads into buffer){
string[] lines = Regex.Split(buffer,System.Enviorment.NewLine);
}
还有其他许多方式,但上面的一个可能是你会去什么。
Answer 5:
你可以快速读取它,并增加一个计数器,只使用一个循环递增,无为与文本。
Answer 6:
计数回车/换行符。 我相信,在Unicode中,它们分别0x000D和0x000A都还在。 这样你就可以有效,或者只要你想低效,并决定是否要对付两个字符或不
Answer 7:
一种可行的选择,和一个我曾亲自使用,将自己的头添加到文件的第一行。 我这样做是为我的游戏自定义模型格式。 基本上,我有一个工具,优化我的.obj文件,摆脱废话我不需要的,将它们转换为一个更好的布局,然后写入线的总数,脸,法线,顶点和纹理的UV的第一行。 加载模型时数据然后被用于通过各种阵列缓冲器。
这也是有用的,因为你只能通过文件需要循环一次,在加载它,而不是一次计数线,并再次将数据读入到您所创建的缓冲区。
Answer 8:
并通过自身读取文件需要一定的时间,垃圾回收的结果是另外一个问题,当你阅读整个文件只是计数换行符(S)
在某些时候,有人会具有读取文件的字符,而不管是否框架,或者如果它是你的代码。 这意味着你必须打开该文件并读入内存,如果该文件是大这将可能是一个问题,因为内存需要进行垃圾回收。
尼玛阿拉做,你可能会考虑一个很好的分析
下面是所提出的解决方案,因为它一次读取4个字符,计数换行符并重新使用相同的存储器地址为下一个字符的比较。
private const char CR = '\r';
private const char LF = '\n';
private const char NULL = (char)0;
public static long CountLinesMaybe(Stream stream)
{
Ensure.NotNull(stream, nameof(stream));
var lineCount = 0L;
var byteBuffer = new byte[1024 * 1024];
const int BytesAtTheTime = 4;
var detectedEOL = NULL;
var currentChar = NULL;
int bytesRead;
while ((bytesRead = stream.Read(byteBuffer, 0, byteBuffer.Length)) > 0)
{
var i = 0;
for (; i <= bytesRead - BytesAtTheTime; i += BytesAtTheTime)
{
currentChar = (char)byteBuffer[i];
if (detectedEOL != NULL)
{
if (currentChar == detectedEOL) { lineCount++; }
currentChar = (char)byteBuffer[i + 1];
if (currentChar == detectedEOL) { lineCount++; }
currentChar = (char)byteBuffer[i + 2];
if (currentChar == detectedEOL) { lineCount++; }
currentChar = (char)byteBuffer[i + 3];
if (currentChar == detectedEOL) { lineCount++; }
}
else
{
if (currentChar == LF || currentChar == CR)
{
detectedEOL = currentChar;
lineCount++;
}
i -= BytesAtTheTime - 1;
}
}
for (; i < bytesRead; i++)
{
currentChar = (char)byteBuffer[i];
if (detectedEOL != NULL)
{
if (currentChar == detectedEOL) { lineCount++; }
}
else
{
if (currentChar == LF || currentChar == CR)
{
detectedEOL = currentChar;
lineCount++;
}
}
}
}
if (currentChar != LF && currentChar != CR && currentChar != NULL)
{
lineCount++;
}
return lineCount;
}
上面可以看到一条线读出一个字符时间,以及由底层框架,你需要阅读所有字符,看换行。
如果您的个人资料作为进行海湾尼玛,你会看到,这是这样做的相当快速和有效的方式。
Answer 9:
try {
string path = args[0];
FileStream fh = new FileStream(path, FileMode.Open, FileAccess.Read);
int i;
string s = "";
while ((i = fh.ReadByte()) != -1)
s = s + (char)i;
//its for reading number of paragraphs
int count = 0;
for (int j = 0; j < s.Length - 1; j++) {
if (s.Substring(j, 1) == "\n")
count++;
}
Console.WriteLine("The total searches were :" + count);
fh.Close();
} catch(Exception ex) {
Console.WriteLine(ex.Message);
}
Answer 10:
可以启动“ WC的.exe”的可执行文件(自带UnixUtils ,不需要安装)运行作为外部进程。 它支持不同的行数的方法(如UNIX VS MAC VS窗口)。