首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > .NET >

跪求各位大佬,帮帮忙吧!转换txt文件的字符编码解决思路

2012-01-07 
跪求各位大佬,帮帮忙吧!!!!!!!!转换txt文件的字符编码读取一个txt文本,然后转换他的编码格式为UTF—8 小弟

跪求各位大佬,帮帮忙吧!!!!!!!!转换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……

热点排行