当前位置:   article > 正文

C# 将GB2312的文件转为UTF-8格式_c# gb2312转utf-8

c# gb2312转utf-8
  1. public void EncryptFile(string strInFileName, string strOutFileName)
  2. {
  3. try
  4. {
  5. byte[] data = File.ReadAllBytes(strInFileName);
  6. StreamWriter sw = new StreamWriter(strOutFileName, false, Encoding.Unicode);
  7. char[] buffer2 = Encoding.Unicode.GetChars(data);
  8. sw.Write(buffer2);
  9. sw.Flush();
  10. sw.Close();
  11. }
  12. catch (Exception ex)
  13. {
  14. throw ex;
  15. }
  16. }
  17. public void DecryptFile(string strInFileName, string strOutFileNameV)
  18. {
  19. StreamReader sr = new StreamReader(strInFileName, System.Text.Encoding.Unicode);
  20. StreamWriter sw = new StreamWriter(strOutFileNameV, false, System.Text.Encoding.GetEncoding("Shift-JIS"));
  21. try
  22. {
  23. char[] buffer;
  24. while (!sr.EndOfStream)
  25. {
  26. buffer = System.Text.Encoding.GetEncoding("Shift-JIS").GetChars(System.Text.Encoding.Unicode.GetBytes(sr.ReadLine()));
  27. sw.WriteLine(buffer);
  28. }
  29. sw.Flush();
  30. sw.Close();
  31. sr.Close();
  32. }
  33. catch (Exception ex)
  34. {
  35. throw ex;
  36. }
  37. }
  38. bool writeunicodecsv()
  39. {
  40. StreamReader sr = new StreamReader(@"D:\q.csv", System.Text.Encoding.GetEncoding("Shift-JIS"));
  41. StreamWriter sw = new StreamWriter(@"D:\qUnicode.csv", false, Encoding.Unicode);
  42. try
  43. {
  44. string strline;
  45. string strWrite;
  46. int i = 0;
  47. while (!sr.EndOfStream)
  48. {
  49. i++;
  50. strline = sr.ReadLine();
  51. strWrite = System.Text.Encoding.Unicode.GetString(System.Text.Encoding.GetEncoding("Shift-JIS").GetBytes(strline));
  52. sw.WriteLine(strWrite);
  53. }
  54. sr.Close();
  55. sw.Flush();
  56. sw.Close();
  57. MessageBox.Show(i.ToString());
  58. }
  59. catch (Exception ex)
  60. {
  61. throw ex;
  62. MessageBox.Show(ex.Message);
  63. }
  64. return true;
  65. }
  66. bool readcsv()
  67. {
  68. string strReadCsvFile = @"D:\qUnicode.csv";
  69. string strWriteCsvFile = @"D:\qshiftjis.csv";
  70. StreamReader sr = new StreamReader(strReadCsvFile, System.Text.Encoding.Unicode);
  71. StreamWriter sw = new StreamWriter(strWriteCsvFile, false, System.Text.Encoding.GetEncoding("Shift-JIS"));
  72. try
  73. {
  74. string strline;
  75. string strWrite;
  76. int i = 0;
  77. while (!sr.EndOfStream)
  78. {
  79. i++;
  80. strline = sr.ReadLine();
  81. strWrite = System.Text.Encoding.GetEncoding("Shift-JIS").GetString(System.Text.Encoding.Unicode.GetBytes(strline));
  82. sw.WriteLine(strWrite);
  83. }
  84. sr.Close();
  85. sw.Flush();
  86. sw.Close();
  87. }
  88. catch (Exception ex)
  89. {
  90. throw ex;
  91. MessageBox.Show(ex.Message);
  92. }
  93. return true;
  94. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/253429
推荐阅读
相关标签
  

闽ICP备14008679号