大众信息网

c#读取与修改utf-8格式的txt文件

答案:2  mip版
解决时间 2021-02-08 07:52
  • 提问者网友:逍遥风
  • 2021-02-07 16:31
没有用。net直接写代码在控制台显示
最佳答案
  • 二级知识专家网友:棄療尐钕
  • 2021-02-07 18:02
using System;
using System.Text;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // 写入内容,然后用utf8编码保存
            string text = 
                 "文件文件的内容,包含中文和英文,Hello world!世界你好!";
            File.WriteAllText(@"d:\a.txt", text, Encoding.UTF8);

            // 从文件中读入内容并显示
            string s = File.ReadAllText(@"d:\a.txt", Encoding.UTF8);
            Console.WriteLine(s);
        }
    }
}
全部回答
  • 1楼网友:娇而不傲的猫
  • 2021-02-07 18:45
public static void writehtmlfile(string shtml, string htmlpath) { streamwriter sw = null; try { utf8encoding utf8 = new utf8encoding(false); using (sw = new streamwriter(htmlpath, false, utf8)) { sw.write(shtml); } } catch { throw new exception("存储路径错误,请检查路径" + htmlpath + "是否存在!"); } finally { sw.close(); } }
我要举报
如以上问答内容为低俗/色情/暴力/不良/侵权的信息,可以点下面链接进行举报,我们会做出相应处理,感谢你的支持!
点此我要举报以上问答信息
推荐资讯