日期:2014-05-20  浏览次数:20703 次

跪求各位大佬,帮帮忙吧!!!!!!!!转换txt文件的字符编码
读取一个txt文本,然后转换他的编码格式为UTF—8 小弟用尽各种方法都没成功!!!!! 我没多少分了,以后追加!!!谢谢各位。。。。
StreamReader sr = new StreamReader(url, System.Text.Encoding.UTF8); //utf8读取文本
  String input = sr.ReadToEnd();
  sr.Close();

  // File.Delete(url);
  File.WriteAllText(url,input,Encoding.UTF8);
  //StreamWriter sw = new StreamWriter(url,false,System.Text.Encoding.UTF8 );
  //sw.Write(input);
  //sw.Close();

这是我写的 都不成功。。。哎!!

------解决方案--------------------
用记事本打开这个文件,如果是乱码那文件不是一个文本文件
StreamReader sr = new StreamReader(file,System.Text.Encoding.GetEncoding("gb2312"))
using(StreamReader srfile = new StreamReader(filename, 编码))
{}
------解决方案--------------------
探讨
读取一个txt文本,然后转换他的编码格式为UTF—8 小弟用尽各种方法都没成功!!!!! 我没多少分了,以后追加!!!谢谢各位。。。。
StreamReader sr = new StreamReader(url, System.Text.Encoding.UTF8); //utf8读取文本
String input = sr.ReadToEnd();
sr.Close();

……

------解决方案--------------------
string file = @"d:\test.txt";
string text = File.ReadAllText(file, Encoding.GetEncoding("gb2312"));
File.WriteAllText(file, text, Encoding.UTF8);

------解决方案--------------------
很好写的,代码如下
C# code

//流文件的操作
            
            string Path = @"c:\zheng.txt";
            FileStream fsr = new FileStream(Path, FileMode.Create);
            Byte[] info = new UTF8Encoding(true).GetBytes("This is shenbao's text!!!");
            fsr.Write(info, 0, info.Length);
            fsr.Close();


            using (StreamReader srr = File.OpenText(Path))
            {
                string strings;
                while ((strings = srr.ReadLine()) != null)
                {
                    Console.WriteLine(strings);
                }
            }
            
            
            Thread.Sleep(5000);

------解决方案--------------------
探讨

很好写的,代码如下
C# code

//流文件的操作

string Path = @"c:\zheng.txt";
FileStream fsr = new FileStream(Path, FileMode.Create);
Byte[] info = new UTF8Encoding(tr……