首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >比较两个文本文件并显示什么是相同的

比较两个文本文件并显示什么是相同的
EN

Stack Overflow用户
提问于 2017-03-21 20:15:08
回答 1查看 3.4K关注 0票数 0

我需要帮助比较两个文件。我能打印出两者之间的区别,但我无法弄清楚如何打印出两者之间的相同之处。有人能帮我吗?提前感谢!

代码语言:javascript
复制
    private void button2_Click(object sender, EventArgs e)
    {
        try
        {
            //Reads files line by line
            var fileOld = File.ReadLines(Old);
            var fileNew = File.ReadLines(New);                              //Ignores cases in files
            IEnumerable<String> OldTxt = fileOld.Except(fileNew, StringComparer.OrdinalIgnoreCase);
            IEnumerable<String> NewTxt = fileNew.Except(fileOld, StringComparer.OrdinalIgnoreCase);

            FileCompare compare = new FileCompare();

            bool areSame = OldTxt.SequenceEqual(NewTxt);

            if (areSame == true)
            {
                MessageBox.Show("They match!");
            }
            else if(areSame == false)
            {
                // Find the set difference between the two files.  
                // Print to Not Equal.txt
                var difFromNew = (from file in OldTxt
                                  select file).Except(NewTxt);

                using (var file = new StreamWriter(NotEqual))
                {
                    foreach (var notEq in difFromNew)
                    {
                        file.WriteLine(notEq.ToString() + "\n", true);
                    }
                }

                MessageBox.Show("They are not the same! Please look at 'Not Equal.txt' to see the difference. Look at 'Equal.txt' to see what is the same at this time.");
            }

        }
        catch
        {
            MessageBox.Show("'NewConvert.txt' or 'OldConvert.txt' files does not exist.");
        }

    }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-03-21 20:58:05

首先,我认为您的代码中可能有一个bug。“fileOld”包含旧文件的所有内容,而“OldTxt”仅包含新文件中不存在的旧文件文本。在我回答之后,请参阅下面的代码清理想法。

我认为您在寻找Intersect,它返回两个IEnumerables共有的项:

代码语言:javascript
复制
var commonItems = fileOld.Intersect(fileNew);

或者,由于您已经有了在difFromNew中捕获的差异列表,所以您可以再次使用Except

代码语言:javascript
复制
var difFromNew = fileOld.Except(fileNew); // Note I fixed what I think is a bug here
var commonItems = fileOld.Except(difFromNew);

一些潜在的缺陷:

  1. SequenceEqual不仅意味着项目相同,而且它们的顺序也是一样的。如果您关心订单,那么这是合适的。问题是,这将使显示差异变得困难,因为用于比较列表(ExceptIntersect) 的其他方法不关心顺序,只关心是否存在项目。因此,如果fileOld包含项cat | dogfileNew包含项dog | cat,那么它们将不是相等的,但您也无法显示差异(fileOld.Except(fileNew)将包含0项)。
  2. diffFromNew列表中,您将使用OldTxt (它是旧文件中的唯一文本),并对NewTxt执行Except,后者是来自新文件的唯一文本。这两者之间不可能有重叠,事实上,OldTxt 默认已经包含了 diffFromNew

有一种方法可以让你找到你想要的清单:

代码语言:javascript
复制
var oldFilePath = "c:\\oldFile.txt";
var newFilePath = "c:\\newFile.txt";

var oldFileTxt = File.ReadLines(oldFilePath);
var newFileTxt = File.ReadLines(newFilePath);

// Determine if they're equal by checking if there are not any unique items
var filesAreSame = !oldFileTxt.Except(newFileTxt, StringComparer.OrdinalIgnoreCase).Any();

var commonItems = oldFileTxt.Intersect(newFileTxt, StringComparer.OrdinalIgnoreCase);
var uniqueOldItems = oldFileTxt.Except(commonItems, StringComparer.OrdinalIgnoreCase);
var uniqueNewItems = newFileTxt.Except(commonItems, StringComparer.OrdinalIgnoreCase);

下面是代码在这些更改中的表现:

代码语言:javascript
复制
if (filesAreSame)
{
    MessageBox.Show("They match!");
}
else
{
    var commonItems = oldFileTxt.Intersect(newFileTxt, StringComparer.OrdinalIgnoreCase);
    var uniqueOldItems = oldFileTxt.Except(commonItems, StringComparer.OrdinalIgnoreCase);
    var uniqueNewItems = newFileTxt.Except(commonItems, StringComparer.OrdinalIgnoreCase);

    var notEqualsFileText = new StringBuilder();
    if (uniqueOldItems.Any())
    {
        notEqualsFileText.AppendLine(
            $"Entries in {oldFilePath} that are not in {newFilePath}:");
        notEqualsFileText.AppendLine(string.Join(Environment.NewLine, uniqueOldItems));
    }
    if (uniqueNewItems.Any())
    {
        notEqualsFileText.AppendLine(
            $"Entries in {newFilePath} that are not in {oldFilePath}:");
        notEqualsFileText.AppendLine(string.Join(Environment.NewLine, uniqueNewItems));
    }

    File.WriteAllText(notEqualFilePath, notEqualsFileText.ToString());

    var equalsFileText = new StringBuilder();
    if (commonItems.Any())
    {
        equalsFileText.AppendLine(
            $"Entries that are common in both {newFilePath} and {oldFilePath}:");
        equalsFileText.AppendLine(string.Join(Environment.NewLine, commonItems));
    }
    else
    {
        equalsFileText.AppendLine(
            $"There are no common entries in both {newFilePath} and {oldFilePath}.");
    }

    File.WriteAllText(equalFilePath, equalsFileText.ToString());

    MessageBox.Show("The files are not the same! Please look at 'Not Equal.txt' to see the difference. Look at 'Equal.txt' to see what is the same at this time.");
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42937311

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档