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

        public void EncryptFile(string strInFileName, string strOutFileName)
        {
            try
            {
                byte[] data = File.ReadAllBytes(strInFileName);
                StreamWriter sw = new StreamWriter(strOutFileName, false, Encoding.Unicode);
                char[] buffer2 = Encoding.Unicode.GetChars(data);
                sw.Write(buffer2);
                sw.Flush();
                sw.Close();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public void DecryptFile(string strInFileName, string strOutFileNameV)
        {
            StreamReader sr = new StreamReader(strInFileName, System.Text.Encoding.Unicode);
            StreamWriter sw = new StreamWriter(strOutFileNameV, false, System.Text.Encoding.GetEncoding("Shift-JIS"));
            try
            {
                char[] buffer;
                while (!sr.EndOfStream)
                {
                    buffer = System.Text.Encoding.GetEncoding("Shift-JIS").GetChars(System.Text.Encoding.Unicode.GetBytes(sr.ReadLine()));
                    sw.WriteLine(buffer);
                }
                sw.Flush();
                sw.Close();
                sr.Close();
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }

        bool writeunicodecsv()
        {
            StreamReader sr = new StreamReader(@"D:\q.csv", System.Text.Encoding.GetEncoding("Shift-JIS"));
            StreamWriter sw = new StreamWriter(@"D:\qUnicode.csv", false, Encoding.Unicode);
            try
            {
                string strline;
                string strWrite;
                int i = 0;
                while (!sr.EndOfStream)
                {
                    i++;
                    strline = sr.ReadLine();
                    strWrite = System.Text.Encoding.Unicode.GetString(System.Text.Encoding.GetEncoding("Shift-JIS").GetBytes(strline));
                    sw.WriteLine(strWrite);

                }
                sr.Close();
                sw.Flush();
                sw.Close();
                MessageBox.Show(i.ToString());
            }
            catch (Exception ex)
            {
                throw ex;
                MessageBox.Show(ex.Message);
            }
            return true;

        }

        bool readcsv()
        {
            string strReadCsvFile = @"D:\qUnicode.csv";
            string strWriteCsvFile = @"D:\qshiftjis.csv";
            StreamReader sr = new StreamReader(strReadCsvFile, System.Text.Encoding.Unicode);
            StreamWriter sw = new StreamWriter(strWriteCsvFile, false, System.Text.Encoding.GetEncoding("Shift-JIS"));
            try
            {
                string strline;
                string strWrite;
                int i = 0;
                while (!sr.EndOfStream)
                {
                    i++;
                    strline = sr.ReadLine();
                    strWrite = System.Text.Encoding.GetEncoding("Shift-JIS").GetString(System.Text.Encoding.Unicode.GetBytes(strline));
                    sw.WriteLine(strWrite);
                }
                sr.Close();
                sw.Flush();
                sw.Close();
            }
            catch (Exception ex)
            {
                throw ex;
                MessageBox.Show(ex.Message);
            }
            return true;
        }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值