写入文本文件并不总是工作/保存

时间:2012-08-27 12:59:20

标签: c# filewriter

我有这个代码比较两个文本文件并将差异写入日志文件但是由于某种原因,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();
}

3 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分: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;
    }

答案 2 :(得分: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你知道包含的是案例和文化敏感吗?

相关问题