0

我有这段代码可以比较两个文本文件并将差异写入日志文件,但由于某种原因,log.txt 文件有时是空白的,即使是用 * 开头的某些行进行测试,这些也不总是写我有吗完成写作后保存文本文件,尽管这并不能解释为什么有时它会起作用,但任何帮助都会很棒

private void compare()
{
  string FilePath = @"c:\snapshot\1.txt";
  string Filepath2 = @"c:\snapshot\2.txt";
  int counter = 0;
  string line;
  string line2;

  var dir = "c:\\snapshot\\log.txt";

  using (FileStream fs = File.Create(dir))
  {
    fs.Dispose();
  }

  StreamWriter dest = new StreamWriter(dir);

  if (File.Exists(FilePath) & File.Exists(Filepath2))
  {
    // Read the file and display it line by line.
    using (var file = File.OpenText(FilePath))
    using (var file2 = File.OpenText(Filepath2))
    {
      while (((line = file.ReadLine()) != null & (line2 = file2.ReadLine()) != null))
      {
        if (line.Contains("*"))
        {
          dest.WriteLine(line2);
        }
        else if (!line.Contains(line2))
        {
          dest.WriteLine(line2);
        }
        counter++;
      }
    }
  } 
  dest.Close();
}
4

3 回答 3

1

一旦您在 StreamReader 上点击 close 语句,缓冲区中剩下的所有内容都应该被写出。如果你遗漏了一些东西,那么可能是因为某种原因你没有到达那条线(即你崩溃了)。此外,如果您在写入文件时(即程序仍在运行时)尝试查看文件,您不一定会看到所有内容(因为它还没有关闭)。

通常,最好在 StreamReader 中使用 using 语句。这应该确保它总是被关闭。

于 2012-08-27T13:08:48.347 回答
0

不确定我是否正确理解了您的比较逻辑,但只要我将比较与整个代码分开,您就可以根据自己的需要进行调整:

    public static void WriteDifferences(string sourcePath, string destinationPath, string differencesPath)
    {
        var sourceLines = File.ReadAllLines(sourcePath).ToList();
        var destinationLines = File.ReadAllLines(destinationPath).ToList();            

        // make lists equal size
        if (sourceLines.Count > destinationLines.Count)
        {
            destinationLines.AddRange(Enumerable.Range(0, sourceLines.Count - destinationLines.Count).Select(x => (string)null));
        } 
        else
        {
            sourceLines.AddRange(Enumerable.Range(0, destinationLines.Count - sourceLines.Count).Select(x => (string)null));
        }

        var differences = sourceLines.Zip(destinationLines, (source, destination) => Compare(source, destination));

        File.WriteAllLines(differencesPath, differences.Where(x => x != null));
    }

    private static string Compare(string source, string destination)
    {
        return !source.Contains(destination) || source.Contains("*") ? destination : null;
    }
于 2012-08-27T13:29:10.363 回答
0
private void compare()
{
  string FileName1 = @"c:\snapshot\1.txt";
  string FileName2 = @"c:\snapshot\2.txt";
  string FileNameOutput = @"c:\snapshot\log.txt"; //dir ???
  int counter = 0; // um what's this for you aren't using it.

  using (FileStream fso = new FileStream(FileNameOutput, FileMode.Create, FileAccess.Write))
  {
    TextWriter dest = new StreamWriter(fso);
    using(FileStream fs1 = new FileStream(FileName1, FileMode.Open, FileAccess.Read))
    {
      using (FileStream fs2 = new FileStream(FileName2, FileMode.Open, FileAccess.Read))
      {
        TextReader firstFile = new StreamReader(fs1);
        TextReader secondFile = new StreamReader(fs2);
        while (((line1 = firstFile.ReadLine()) != null & (line2 = secondFile.ReadLine()) != null))
        {
          if ((line1.Contains("*") || (!line1.Contains(line2)))
          {
            dest.Write(line2); // Writeline would give you an extra line?
          }
          counter++; // 
        }
      }
    }
  fso.Flush();
}

我向您推荐 FileStream 的重载。按照我的方式进行操作,如果运行它的用户没有所有必需的权限,则代码将在您实例化流时崩溃。这是一个很好的方式来展示你想要什么,你不想要什么。

PS您知道包含区分大小写和文化吗?

于 2012-08-27T14:14:11.313 回答