小编May*_*hav的帖子

如何循环并比较两个文本文件中的数百万个值?

我有两个文本文件文件(TXT),其中包含超过200万个不同的文件名.我想遍历第一个文件中的所有名称,并找到第二个文本文件中也存在的名称.

我试过循环,StreamReader但需要花费很多时间.我也尝试了下面的代码,但它仍然需要太多时间.

StreamReader first = new StreamReader(path);
string strFirst = first.ReadToEnd();
string[] strarrFirst = strFirst.Split('\n');

 bool found = false;

StreamReader second = new StreamReader(path2);
string str = second.ReadToEnd();
string[] strarrSecond = str.Split('\n');

for (int j = 0; j < (strarrFirst.Length); j++)
{
          found = false;

    for (int i = 0; i < (strarrSecond .Length); i++)
    {
        if (strarrFirst[j] == strarrSecond[i])
        {
            found = true;
            break;
        }
    }

    if (!found)
    {
        Console.WriteLine(strarrFirst[j]);
    }
}
Run Code Online (Sandbox Code Playgroud)

有什么比较文件的好方法?

c# arrays performance loops data-structures

2
推荐指数
1
解决办法
1571
查看次数

标签 统计

arrays ×1

c# ×1

data-structures ×1

loops ×1

performance ×1