C# 文件读取行计数
我试图从一个文件中读取数据,程序会遍历文本文件的每一行,比较每一行的前8个字符,并将这些行合并成一个8个字符相似的行。请参阅代码:C# 文件读取行计数,c#,C#,我试图从一个文件中读取数据,程序会遍历文本文件的每一行,比较每一行的前8个字符,并将这些行合并成一个8个字符相似的行。请参阅代码: while ((line1 = fileread1.ReadLine()) != null) { line2 = fileread2.ReadLine(); while (line2 != null) { if (line1.Length &g
while ((line1 = fileread1.ReadLine()) != null)
{
line2 = fileread2.ReadLine();
while (line2 != null)
{
if (line1.Length >= 8 && line2.Length >= 8 &&
line1.Substring(0, 8) == line2.Substring(0, 8))
{
//line2 = line2.Remove(0, 60);
line1 = line1 +" "+ line2;
}
line2 = fileread3.ReadLine();
counter2++;
}
filewrite.WriteLine(line1);
counter1++;
}
问题1:
如何获取fileread2的计数并将其分配给fileread3,因为我需要在每次执行内部循环时将fileread3的计数重置为与fileread2相同
问题2:
如何将前8个字符匹配的组合行写成单行。从阅读注释中,我了解到您实际上想要删除重复的行。或者,更具体地说,以相同的8个字符开头的行 如果是这种情况,为什么不使用一组键来记住是否已经加载了具有相同8个初始字符的字符串 例如,您可以在一个集合(如列表)中添加8个字符的子字符串。然后,在写入行之前,首先检查子字符串是否已在集合中。如果是,那么你知道这是一个副本,你不写它
另一个选择是,如果您想计算特定类型的副本数量,可以使用字典。当您有一个特定类型的副本时,您可以增加字典中的计数器。如果您想根据前8个字符对数据进行分组,您只需要:
var groups = File.ReadLines(your_file_name).GroupBy(line => line.Substring(0, 8));
要对组中的元素计数,请调用count()
groups.First().Count()
(您还可以通过foreach(组中的var组)
枚举组
。)
组结果将包含文件中的完整行。如果您不希望结果中出现“key”(即前8个字符),可以使用GroupBy重载
var groups = File.ReadLines(your_file_name).GroupBy(line => line.Substring(0, 8), line => line.Substring(8));
为了获得带有“键”和连接行的新输出,您可以使用如下内容:
var groups = File.ReadLines(input_file).GroupBy(line => line.Substring(0, 8), line => line.Substring(8));
File.WriteAllLines(output_file, groups.Select(group => string.Format("{0}{1}", group.Key, string.Join(string.Empty, group))));
由于您有大文件,可能
File.ReadAllLines
不是您的选项。您可以声明以下方法(我们需要使用Stream
,因为StreamReader
没有Position
属性):
publicstaticstringreadline(流)
{
返回ReadLine(流,编码.UTF8);
}
公共静态字符串读线(流、编码)
{
List lineBytes=新列表();
while(stream.Position
示例代码:
string sourceFileName = "input.txt";
string targetFileName = "output.txt";
using (StreamWriter targetWriter = new StreamWriter(targetFileName))
using (FileStream sourceStream = File.OpenRead(sourceFileName))
{
HashSet<string> processedKeys = new HashSet<string>();
while (sourceStream.Position < sourceStream.Length)
{
string line = ReadLine(sourceStream);
if (line.Length < 8)
targetWriter.WriteLine(line);
else
{
string key = line.Substring(0, 8);
if (processedKeys.Contains(key))
continue;
targetWriter.Write(line);
long backupPosition = sourceStream.Position;
while (sourceStream.Position < sourceStream.Length)
{
string dupLine = ReadLine(sourceStream);
if (dupLine.Length < 8)
continue;
string dupKey = dupLine.Substring(0, 8);
if (dupKey == key)
targetWriter.Write(" " + dupLine);
}
sourceStream.Position = backupPosition;
targetWriter.WriteLine();
processedKeys.Add(key);
}
}
}
string sourceFileName=“input.txt”;
字符串targetFileName=“output.txt”;
使用(StreamWriter targetWriter=新StreamWriter(targetFileName))
使用(FileStream sourceStream=File.OpenRead(sourceFileName))
{
HashSet processedKeys=新HashSet();
while(sourceStream.Position
o_o?!说什么?!fileread[1..3]是关于什么的?你到底想做什么?听起来你可以用一点LINQ,但我不确定你想做什么…我使用的逻辑是从第一行开始选择每一行,然后对其余的行进行比较。这就是fileread 1和2的原因。fileread 3循环遍历整行并到达文件的末尾,所以除非我重置它的计数,否则我无法继续使用。@user3631366这不是一个真正的计数,这是一个寻找的位置。另外,看起来您正在比较多个文件?或者FileRead1..3都指向同一个文件吗?@James Barrass是的,它们指向同一个文件,但在这个文件中有不同的行。选择第1行并与第2行进行比较,如果相同的组合,如果没有选择第3行,则按顺序选择4、5、6等。@TariqOndego您希望这些文件有多大?我如何将这些组写入文本文件?每行一组?你只想用键将字符串凹化吗?i、 e.如果您有“我的名字是约翰”和“我的名字是多伊”,那么输出应该是“我的名字是约翰·多伊”或“我的名字是约翰·我的名字是多伊”。让我知道,我会完成回答。应该是“我的名字是john doe”。文件有两个标题,我如何用读线跳过这两个标题,因为它们不满足8个字符长度的条件。然后我需要将标题放回输出文件。好的,您可以使用.skip()
和.Take()
,也在LINQ库中提供。或者你可以重写逻辑,这样你就不用GroupBy
而是foreach
覆盖文件了。ReadLines
ReadLines
的工作原理与ReadAllLines
不同,因为当你使用ReadLines时,你可以在返回整个集合之前开始枚举字符串集合;按照。使用您在文件
中提供的ReadLine
方法的任何原因?@Default我需要在搜索重复项时备份和还原位置
属性。我不知道如何通过读线
实现。关于您提供的示例代码:GroupBy
string sourceFileName = "input.txt";
string targetFileName = "output.txt";
using (StreamWriter targetWriter = new StreamWriter(targetFileName))
using (FileStream sourceStream = File.OpenRead(sourceFileName))
{
HashSet<string> processedKeys = new HashSet<string>();
while (sourceStream.Position < sourceStream.Length)
{
string line = ReadLine(sourceStream);
if (line.Length < 8)
targetWriter.WriteLine(line);
else
{
string key = line.Substring(0, 8);
if (processedKeys.Contains(key))
continue;
targetWriter.Write(line);
long backupPosition = sourceStream.Position;
while (sourceStream.Position < sourceStream.Length)
{
string dupLine = ReadLine(sourceStream);
if (dupLine.Length < 8)
continue;
string dupKey = dupLine.Substring(0, 8);
if (dupKey == key)
targetWriter.Write(" " + dupLine);
}
sourceStream.Position = backupPosition;
targetWriter.WriteLine();
processedKeys.Add(key);
}
}
}