向文件中写入字符时出错了 急啊!!!
using System;
using System.Text;
using System.IO;
namespace 文件操作
{
class Program
{
static void Main(string[] args)
{
byte[] m_bDataWrite = new byte[100];
char[] m_cDataWrite = new char[100];
try
{
FileStream m_filestream = new FileStream(@ "G:\file.txt ", FileMode.OpenOrCreate);
m_cDataWrite = "My First File Operation ".ToCharArray();
Encoder m_enc = Encoding.UTF8.GetEncoder();
m_enc.GetBytes(m_cDataWrite, 0, m_cDataWrite.Length, m_bDataWrite,0,true);
m_filestream.Seek(0, SeekOrigin.Begin);
m_filestream.Write(m_bDataWrite, 0, m_bDataWrite.Length);
}
catch (IOException e)
{
Console.WriteLine(e.ToString());
}
Console.WriteLine( "write to file succeed! ");
Console.ReadLine();
}
}
}
各位大哥 我为什么在文件中写不进去东西啊? 恳请告知哪错了
------解决方案--------------------m_filestream.Seek(0, SeekOrigin.Begin);
m_filestream.Write(m_bDataWrite, 0, m_bDataWrite.Length);
//增加下面的代码
m_filestream.Flush();//清除缓冲区
m_filestream.Close();//关闭文件流