【问题标题】:Why doesn't FileStream as an argument to Streamwriter write to text file?为什么 FileStream 不作为 Streamwriter 的参数写入文本文件?
【发布时间】:2014-08-14 18:59:34
【问题描述】:

在下面包含的代码中,当使用以下语句时,我可以将字符串“全名”的内容写入指定目录中的文本文件: System.IO.File.WriteAllText(path, fullname); 但是,如果我将字符串路径写入 FileStream 对象(指定参数),然后将该 FileStream 对象作为参数传递给 StreamWriter 对象,则会创建文件,但不会写入任何内容。

第一次尝试:注释掉System.IO.File.WriteAllText(path, fullname); 并使用上面的三行。这将创建文件,但没有内容写入文件。

第二次尝试:取消注释System.IO.File.WriteAllText(path, fullname); 语句并注释它上面的三行。这会根据需要执行。

这是完整的代码块:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace FileInputOutput
{
    class Program
    {
        static void Main(string[] args)
        {
            // Use the Split() method of the String Class
            string fullname = " Robert Gordon Orr ";
            fullname = fullname.Trim();
            string[] splitNameArray = fullname.Split(' ');
            Console.WriteLine("First Name is: {0}", splitNameArray[0]);
            Console.WriteLine("Middle Name is: {0}", splitNameArray[1]);
            Console.WriteLine("Last Name is: {0}", splitNameArray[2]);
            Console.WriteLine("Full name is: {0}", fullname);
            string path = @"C:\Programming\C#\C# Practice Folder\Console Applications\FileInputOutput\textfile.txt";

            FileStream fs = new FileStream(path, FileMode.Create, FileAccess.ReadWrite);
            StreamWriter toFile = new StreamWriter(fs);
            toFile.Write(fullname);

            //System.IO.File.WriteAllText(path, fullname);`enter code here`

            Console.ReadLine();

        }
    }
}

【问题讨论】:

  • 你可以改用File.WriteAllText..
  • 那是因为你看文件的时间不对。 FileStream 尚未刷新其缓冲区。它没有任何理由这样做,您没有关闭文件。在这里使用 using 语句不是可选的。
  • 欢迎您!当你不以关于你的经历或你的研究的陈述开头时,问题通常看起来更好。最好从您的实际问题(感叹号后的所有内容)开始,然后在最后包含您研究中的相关引述和指向您的来源的链接。

标签: c# filestream streamwriter


【解决方案1】:

正如其他人所说:必须在 .NET 中刷新流才能写入磁盘。这可以手动完成,但是我只需将您的代码更改为在您的流上使用 using 语句:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace FileInputOutput
{
    class Program
    {
        static void Main(string[] args)
        {
            // Use the Split() method of the String Class
            string fullname = " Robert Gordon Orr ";
            fullname = fullname.Trim();
            string[] splitNameArray = fullname.Split(' ');
            Console.WriteLine("First Name is: {0}", splitNameArray[0]);
            Console.WriteLine("Middle Name is: {0}", splitNameArray[1]);
            Console.WriteLine("Last Name is: {0}", splitNameArray[2]);
            Console.WriteLine("Full name is: {0}", fullname);
            string path = @"C:\textfile.txt";

            using (FileStream fs = new FileStream(path, FileMode.Create, FileAccess.ReadWrite))
            {
                using (StreamWriter toFile = new StreamWriter(fs))
                {
                    toFile.Write(fullname);
                }
            }
            //System.IO.File.WriteAllText(path, fullname);`enter code here`

            Console.ReadLine();

        }
    }
}

在流上调用 Dispose()(与 using 隐式一样)会导致流在 using 块的末尾被刷新和关闭。

【讨论】:

  • 谢谢大家。我对在 ADO 数据库调用中应用它的“使用”语句有点熟悉。使用上面的“使用”代码块后,我的文件已成功写入。我将对此进行进一步调查,以更好地了解执行情况。再次感谢大家。
【解决方案2】:

From MSDN on StreamWriter

您必须调用 Close 以确保所有数据都正确写入底层流。

所以这里的问题主要在于,由于您实际上并没有关闭 StreamWriter,因此即使 FileStream 立即在其构造函数中创建了文件,数据也会备份但不会推送到文件中。永远不要忘记关闭您的信息流,否则可能会导致严重的问题。

【讨论】:

    【解决方案3】:

    我认为您只是忘记刷新文件流:

    fs.Flush();
    

    这是必需的,因为根据 msdn,这是使 FileStream 实际将缓冲区写入文件的原因。

    Flush:清除此流的缓冲区并导致将任何缓冲数据写入文件。 (覆盖 Stream.Flush()。)

    问候。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-01-20
      • 2018-04-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-02
      相关资源
      最近更新 更多