C# ANSI格式 另存 UTF8

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wangtao510/article/details/83584851

ANSI 另存 UTF8

  private void AnsiToUtf8(string rbFile)
        {

           //rbFile Ansi格式的文件路径
            FileStream fs = new FileStream(rbFile, FileMode.Open, FileAccess.Read);
            StreamReader sr = new StreamReader(fs, Encoding.Default);

            //另存的文件路径
            string txtFile = Path.GetDirectoryName(rbFile) + "\\" + Path.GetFileNameWithoutExtension(rbFile) + "_Copy.txt";
            FileStream fsw = new FileStream(txtFile, FileMode.Create, FileAccess.Write);

            //另存为UTF-8
            StreamWriter sw = new StreamWriter(fsw, Encoding.UTF8);
            char[] charArr = new char[100];
            int count = sr.Read(charArr, 0, 100);
            while (count != 0)
            {
                sw.Write(charArr, 0, 100);
                count = sr.Read(charArr, 0, 100);
            }

            sw.Close();
            sr.Close();
            fsw.Close();
            fs.Close();  
        }

猜你喜欢

转载自blog.csdn.net/wangtao510/article/details/83584851