English 中文(简体)
页: 1
原标题:Can t read international characters from files

I am trying to read portuguese characters from files, and keep getting into problems.

我有以下C#代码(用于测试):

var streamReader = new StreamReader("file.txt");

while (streamReader.Peek() >= 0)
{
  var buffer = new char[1];
  streamReader.Read(buffer, 0, buffer.Length);
  Console.Write(buffer[0]);
}

It reads each character in the file and then outputs it to the console. The file contains the following: "cãsa". The output in the console is: "c?sa".

我做了什么错误?

最佳回答

你们需要用正确的编码阅读档案,否则,如果档案不是正确的编码,你就会发现这些问题。

举例来说,我使用的是超载的构件,在此情况下是UnicodeEncoding,即UTF-16:

using(var streamReader = new StreamReader("file.txt", Encoding.UnicodeEncoding))
{
    while (streamReader.Peek() >= 0)
    {
      var buffer = new char[1];
      streamReader.Read(buffer, 0, buffer.Length);
      Console.Write(buffer[0]);
    }
}

例如,我使用第860号法典,相当于葡萄牙文:

using(var streamReader = new StreamReader("file.txt", Encoding.GetEncoding(860)))
{
    while (streamReader.Peek() >= 0)
    {
      var buffer = new char[1];
      streamReader.Read(buffer, 0, buffer.Length);
      Console.Write(buffer[0]);
    }
}
问题回答

暂无回答




相关问题
How does gettext handle dynamic content?

In php (or maybe gettext in general), what does gettext do when it sees a variable to dynamic content? I have 2 cases in mind. 1) Let s say I have <?=$user1?> poked John <?=$user2?>. ...

Explain the Need for Mutexes in Locales, Please

Reading the question Why doesn’t C++ STL support atoi(const string& ) like functions?, I encountered a comment which warned that GCC (at least) has a bug that can slow down multi-threaded ...

How does Vistalizer work

How does Vistalizer manage to override the language limit in Windows Vista Home edition. Which api s does it use to allow installation of Multiple language packages.

Localized exceptions (within a Struts2 app)

I am developing a Struts 2 application with support for multiple languages. If one of the domain objects needs to throw an exception, how can it do so in such a way that the error message is no ...

Rails Globalize plugin help

Has anyone gotten the Globalize plugin to work Rails 2.3.2 or later? If so, could you direct me to some useful info?

热门标签