Filestream.write不起作用

时间:2015-04-18 08:14:20

标签: c# file filestream

我使用以下代码在*.txt文件上写,但没有任何反应。甚至,也不例外。

        FileStream fs = new FileStream(@"D:\file.txt",FileMode.OpenOrCreate,FileAccess.Write,FileShare.None);    //Creating a stream with certain features to a file

        StreamWriter writer = new StreamWriter(fs);    //Use the fs to write

      //  writer.WriteLine(Text.Text);  none of the following methods works
        writer.Write("aaaaaaaaaaaa");

        fs.Close();

由于

1 个答案:

答案 0 :(得分:3)

尝试将其封装在这样的使用块中:

using ( FileStream fs = new FileStream(@"D:\file.txt",FileMode.OpenOrCreate,FileAccess.Write,FileShare.None))
using (StreamWriter fw = new StreamWriter(fs))
{
    fw.Write("aaaaaaaaaaaa");
} 

StreamWriter在将数据写入基础流之前缓冲数据。您需要通过部署StreamWriter

来刷新缓冲区