asp.net 解決文本中文亂碼 各類文本編碼轉UTF-8 文本

來源:互聯網
上載者:User

標籤:文本中文亂碼解決

 /// <summary>

        /// 解決文本中文亂碼   各類文本編碼轉UTF-8 文本

        /// </summary>

        /// <param name="fullPath">原路徑</param>

        /// <param name="savePath">臨時儲存路徑</param>

        /// <param name="strnew">新的檔案路徑</param>

        /// <returns>新檔案路徑</returns>

        public static string  GetUtf8(string fullPath,string savePath,string strnew)

        {

                             Encoding e =GetFileEncodeType(fullPath);

                                System.IO.File.Create(savePath).Close();

                     using (StreamReader sr = new StreamReader(fullPath, e, false))

                                {

                              string utf8info = string.Empty;


                            Encoding utf8 = Encoding.UTF8;


                             // Convert the string into a byte[]. 

                           byte[] unicodeBytes = e.GetBytes(sr.ReadToEnd());

                           // Perform. the conversion from one encoding to the other. 

                           byte[] asciiBytes = Encoding.Convert(e, utf8, unicodeBytes);


                         // Convert the new byte[] into a char[] and then into a string. 

                        // This is a slightly different approach to converting tillustrate 

                      // the use of GetCharCount/GetChars. 

        char[] asciiChars = new char[utf8.GetCharCount(asciiBytes, 0, asciiBytes.Length)];

         utf8.GetChars(asciiBytes, 0, asciiBytes.Length, asciiChars, 0);

         utf8info = new string(asciiChars);

         using (StreamWriter sw = new StreamWriter(savePath, false, Encoding.UTF8))

                                    {

                                        sw.Write(utf8info);

                                    }

                                }

                                System.IO.File.Delete(fullPath);

                                System.IO.File.Copy(savePath,strnew,true);

                                System.IO.File.Delete(savePath);

                                return strnew;

        }







方法2


                    using (FileStream fs = new FileStream(fullPath, FileMode.Open))

                                {

                           using (StreamReader sr = new StreamReader(fs,Encoding.Default))

                                    {


                                        while (!sr.EndOfStream)

                                        {

                                            string sLine = sr.ReadLine();

                                            //StringBuilder b = new StringBuilder();

                                            //b.AppendLine(sLine);

                                            //另存到其他txt檔案中   


                using (StreamWritersw=System.IO.File.AppendText(savepath))

                                            {

                                                //string n = b.ToString();

                                                sw.WriteLine(sLine.ToString());

                                            }

                                        }


                                    }



                                }

                                System.IO.File.Delete(fullPath);

                                string strNewFile = fullPath;

                                System.IO.File.Copy(savepath, strNewFile, true);

                                System.IO.File.Delete(savepath);

           //using (StreamReader sr = new StreamReader(strNewFile,Encoding.Default, false))

                                //{

              //    using (StreamWriter sw = new StreamWriter(fullPath, falsEncoding.UTF8))

                                //    {

                                //        sw.Write(Server.HtmlEncode(sr.ReadToEnd()));

                                //    }

                                //}

                                //System.IO.File.Delete(strNewFile);

                                FileInfo fileNew = new FileInfo(strNewFile);

                                sourcesFile.File_Size = fileNew.Length;

                                this.sourcesFileService.Save(sourcesFile);

                            }



















asp.net 解決文本中文亂碼   各類文本編碼轉UTF-8 文本

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.