C#写入文件加上bom头,主要适用于utf8文件
C#写入文件加上bom头,主要适用于utf8文件此类可结合C#自动识别文件编码类使用using System;
using System.IO;
public class fileutils{
/// <summary>
/// 写入文件加上bom头,主要适用于utf8文件
/// 欢迎该问http://bbs.agoit.com
/// </summary>
/// <param name="newfilepath">文件地址+文件名</param>
/// <param name="newfilecontent">文件内容</param>
public static void writefilewidthbom(string newfilepath, string newfilecontent)
{
using (FileStream fs = File.OpenWrite(newfilepath))
{
//设定书写的开始位置为文件的末尾
fs.Position = 0;
//先写入bom头
byte[] bomBuffer = new byte[] { 0xef, 0xbb, 0xbf };
//将待写入内容追加到文件末尾
fs.Write(bomBuffer, 0, bomBuffer.Length);
}
using (StreamWriter fsw = File.AppendText(newfilepath))
{
fsw.WriteLine(newfilecontent);
fsw.Close();
}
}
}
页:
[1]