是否有一种简单的方法来编程确定文本文件中的行数?
当前回答
读取一个文件本身需要一些时间,垃圾收集的结果是另一个问题,因为你读取整个文件只是为了计算换行符,
在某些情况下,某人将不得不读取文件中的字符,无论这是框架还是您的代码。这意味着您必须打开文件并将其读入内存,如果文件很大,这将潜在地成为一个问题,因为内存需要进行垃圾收集。
尼玛·阿拉做了一个很好的分析,你可以考虑一下
下面是提出的解决方案,因为它一次读取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;
}
上面你可以看到,一行是由底层框架一次读取一个字符,因为你需要读取所有字符来查看换行。
如果你侧写它为完成海湾尼玛,你会看到这是一个相当快速和有效的方式来做这件事。
其他回答
一个可行的选择,也是我个人使用过的,就是在文件的第一行添加您自己的头文件。我为自己的游戏定制模型格式。基本上,我有一个工具来优化我的.obj文件,去掉我不需要的垃圾,将它们转换为更好的布局,然后在第一行上写入行、面、法线、顶点和纹理uv的总数。当模型加载时,这些数据将被各种数组缓冲区使用。
这也很有用,因为您只需要循环文件一次来加载它,而不是一次来计算行数,然后再次将数据读入创建的缓冲区。
这将使用更少的内存,但可能需要更长的时间
int count = 0;
string line;
TextReader reader = new StreamReader("file.txt");
while ((line = reader.ReadLine()) != null)
{
count++;
}
reader.Close();
用这个:
int get_lines(string file)
{
var lineCount = 0;
using (var stream = new StreamReader(file))
{
while (stream.ReadLine() != null)
{
lineCount++;
}
}
return lineCount;
}
最简单的:
int lines = File.ReadAllLines("myfile").Length;
读取一个文件本身需要一些时间,垃圾收集的结果是另一个问题,因为你读取整个文件只是为了计算换行符,
在某些情况下,某人将不得不读取文件中的字符,无论这是框架还是您的代码。这意味着您必须打开文件并将其读入内存,如果文件很大,这将潜在地成为一个问题,因为内存需要进行垃圾收集。
尼玛·阿拉做了一个很好的分析,你可以考虑一下
下面是提出的解决方案,因为它一次读取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;
}
上面你可以看到,一行是由底层框架一次读取一个字符,因为你需要读取所有字符来查看换行。
如果你侧写它为完成海湾尼玛,你会看到这是一个相当快速和有效的方式来做这件事。
推荐文章
- 返回文件在ASP。Net Core Web API
- 自定义HttpClient请求头
- 如果我使用OWIN Startup.cs类并将所有配置移动到那里,我是否需要一个Global.asax.cs文件?
- VS2013外部构建错误"error MSB4019: The imported project <path> was not found"
- 从另一个列表id中排序一个列表
- 等待一个无效的异步方法
- 无法加载文件或程序集…参数不正确
- c#中枚举中的方法
- 如何从字符串中删除新的行字符?
- 如何设置一个默认值与Html.TextBoxFor?
- 检查属性是否有属性
- 格式化XML字符串以打印友好的XML字符串
- 返回内容与IHttpActionResult非ok响应
- 复选框输入是否只在被选中时才发布数据?
- 从IEnumerable<KeyValuePair<>>重新创建字典