如何在C#.net中读取unicode文本文件 [英] how to read a unicode text file in C#.net

查看:441
本文介绍了如何在C#.net中读取unicode文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想阅读一个unicode文本文件。



代码是:

 私有  void  button1_Click( object  sender,EventArgs e)
{
尝试
{
使用(StreamReader sr = new StreamReader( F:/car_ads.txt ,System.Text.Encoding.Unicode))
{

string line = sr.ReadLine();
textBox1.Text = line;
}
}
catch (例外情况)
{
textBox1.Text = 无法读取文件;


}

}



当我运行此代码时...给我..可以不读取文件。

plz帮我

解决方案

没有这样的编码,Unicode。 Unicode标准的核心部分没有定义任何计算机演示,但UTF定义了一些。没有一个UTF被称为Unicode,但这个荒谬而令人困惑的词语在脏的微软行话中意味着UTF-16LE。此UTF不是文件中使用最多的UTF,但它对应于内存中字符串内部表示的UTF。对于文件,UTF-8更典型。通常,具体的UTF可以由BOM确定。请参阅:

http://unicode.org/faq/utf_bom.html [< a href =http://unicode.org/faq/utf_bom.htmltarget =_ blanktitle =New Window> ^ ]。



这是使用BOM时的通用代码(缺少BOM将被视为ANSI):

 使用(StreamReader reader =  new  StreamReader(fileName, true )){ //   true为自动检测 
string 行= reader.ReadLine();
// 依此类推......
} // 此处,reader.Dispose自动调用

请参阅:https://msdn.microsoft.com/en-us/library/7bc2hwcb%28v=vs.110 %29.aspx [ ^ ]。



有时,UTF在没有BOM的情况下使用。在这种病态情况下,请从此列表中尝试不同的编码:

https://msdn.microsoft.com/en-us/library/system.text.encoding%28v=vs.110%29.aspx [ ^ ] 。



如果编码不是Unicode,那该怎么办?然后猜猜并尝试别的东西。以下是快速方法:将文件重命名为.HTML,使用浏览器打开并使用其自动检测功能,如果您知道该语言。



-SA

i want to read a unicode text file.

code is:

private void button1_Click(object sender, EventArgs e)
       {
           try
           {
               using (StreamReader sr = new StreamReader("F:/car_ads.txt", System.Text.Encoding.Unicode))
               {
                  
                   string line = sr.ReadLine();
                   textBox1.Text = line;
               }
           }
           catch (Exception ex)
           {
               textBox1.Text = "Could not read the file";


           }

       }


when i m running this code ..its giving me .. could not read the file.
plz help me

解决方案

There is no such encoding, "Unicode". Core part of Unicode standard does not define any computer presentation, but UTFs define some. None of the UTF's are called "Unicode", but this absurd and confusing word in dirty Microsoft jargon means "UTF-16LE". This UTF is not the most used in files, but it corresponds to the UTF for internal representation of strings in memory. For files, UTF-8 is more typical. Very usually, concrete UTF can be determined by BOM. Please see:
http://unicode.org/faq/utf_bom.html[^].

This is the universal code in case BOM is used (absent BOM will be treated as ANSI):

using (StreamReader reader = new StreamReader(fileName, true)) { // true is auto-detection
   string line = reader.ReadLine(); 
   // and so on...
} // here, reader.Dispose is automatically called

Please see: https://msdn.microsoft.com/en-us/library/7bc2hwcb%28v=vs.110%29.aspx[^].

Sometimes, UTF is used without the BOM. In such pathological cases, try out different encodings from this list:
https://msdn.microsoft.com/en-us/library/system.text.encoding%28v=vs.110%29.aspx[^].

What if the encoding is not Unicode one? Then take a guess and try something else. Here is the quick way: rename the file to .HTML, open with the browser and use its auto-detection feature, if you know the language.

—SA


这篇关于如何在C#.net中读取unicode文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆