日期:2014-05-18  浏览次数:20780 次

FileStream对象读取文本文件中的文字?
如题,代码如下。
  class ReadFile
  {
  static void Main(string[] args)
  {
  byte[] byteArray = new byte[100];
  char[] charArray = new char[100];

  try
  {
  FileStream aFile = new FileStream(@"E:\temp.txt",FileMode.Open);
  aFile.Read(byteArray, 0, 100);
  }
  catch
  {
  }

  Decoder d = Encoding.UTF8.GetDecoder();
  d.GetChars(byteArray, 0, byteArray.Length, charArray, 0);

  Console.WriteLine(charArray);
  Console.ReadKey();
  }
  }
  在文件中放英文可以显示,放中文不能,显示为乱码。我知道这是解码的问题,不是UTF8格式,不过换成其他格式也不行,请问怎么解决能显示汉字?谢谢了。

------解决方案--------------------
C# code
Decoder d = Encoding.Default.GetDecoder();