大众信息网

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

关注:139  答案:2  手机版
解决时间 2021-02-08 07:52
没有用。net直接写代码在控制台显示
最佳答案
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);
        }
    }
}
全部回答
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(); } }
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!