.net 读取和写入文件,bom字符 System.Text.UTF8Encoding
StringBuilder _strb = new StringBuilder();
FileStream fs = new FileStream(Server.MapPath(_path),FileMode.OpenOrCreate, FileAccess.Read);
char[] cs = new char[fs.Length];
byte[] bs = new byte[fs.Length];
fs.Read(bs, 0, (int)fs.Length);
//Encoding e = System.Text.UTF8Encoding.UTF8;
Encoding e =new System.Text.UTF8Encoding(false);
cs = e.GetChars(bs);
foreach (char c in cs)
{
_strb.Append(c);
}
this.txtContent.InnerText = _strb.ToString();
读取二:
Encoding e = new System.Text.UTF8Encoding(false); //可以不要这句
this.txtContent.InnerText = File.ReadAllText(Server.MapPath(_path), e);
==============================================================
保存一:保存到纯utf8
System.Text.UTF8Encoding utf8 = new System.Text.UTF8Encoding(false);
File.WriteAllText(FilePath, strContent, utf8);
保存二:
FileStream fs = new FileStream(Server.MapPath(_path), FileMode.Truncate);
string str = this.txtContent.InnerText;
byte[] array = new byte[str.Length];
System.Text.Encoding e = System.Text.UTF8Encoding.UTF8;
array = e.GetBytes(str);
fs.Write(array, 0, array.Length);