A simple class that reads UTF-8 or GBK encoding text file
  1. public static class TextFileReader
  2. {
  3.     public static string ReadFile(string path)
  4.     {
  5.         FileStream stream = new FileStream(path, FileMode.Open, FileAccess.Read,
  6.                             FileShare.Read, 8192, FileOptions.SequentialScan);
  7.         return ReadStream(stream);
  8.     }
  9.  
  10.     public static string ReadStream(Stream stream)
  11.     {
  12.         byte[] bytes = new byte[stream.Length];
  13.         long length = (stream.Length > 8192) ? (long)8192 : stream.Length;
  14.         byte first;
  15.         long pos = 0;
  16.         bool is_utf8 = true;
  17.         while (pos < length)
  18.         {
  19.             first = bytes[pos++] = (byte)stream.ReadByte();
  20.             if (first < 192)
  21.             {
  22.             }
  23.             else if (first < 224)
  24.             {
  25.                 if ((length - pos > 1) &&
  26.                     (bytes[pos++] = (byte)stream.ReadByte()) < 128)
  27.                 {
  28.                     is_utf8 = false;
  29.                     break;
  30.                 }
  31.             }
  32.             else if (first < 240)
  33.             {
  34.                 if ((length - pos > 2) &&
  35.                     !((bytes[pos++] = (byte)stream.ReadByte()) > 127
  36.                     && (bytes[pos++] = (byte)stream.ReadByte()) > 127))
  37.                 {
  38.                     is_utf8 = false;
  39.                     break;
  40.                 }
  41.             }
  42.             else
  43.             {
  44.                 if ((length - pos > 3) &&
  45.                     !((bytes[pos++] = (byte)stream.ReadByte()) > 127
  46.                     && (bytes[pos++] = (byte)stream.ReadByte()) > 127
  47.                     && (bytes[pos++] = (byte)stream.ReadByte()) > 127))
  48.                 {
  49.                     is_utf8 = false;
  50.                     break;
  51.                 }
  52.             }
  53.         }
  54.         if (pos < stream.Length)
  55.         {
  56.             stream.Read(bytes, (int)pos, (int)(stream.Length - pos));
  57.         }
  58.         if (is_utf8)
  59.         {
  60.             return Encoding.UTF8.GetString(bytes);
  61.         }
  62.         else
  63.         {
  64.             return Encoding.Default.GetString(bytes);
  65.         }
  66.     }
  67. }
Current language: English · also available in: Chinese (Simplified)
MonoTorrent
MonoTorrent - A BitTorrent Library for .NET
Current language: Chinese (Simplified)
More entries: [1]
« Previous page · Next page »