Read a text file using Encoding.UTF8 in CSharp

Description

The following code shows how to read a text file using Encoding.UTF8.

Example


  /*from   w  w w. j  a  v  a 2 s.  com*/

using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;

public class MainClass
{
    public static void Main()
    {
        Stream s = new FileStream("c:\\test.txt", FileMode.Open);
        using (StreamReader sr = new StreamReader(s, Encoding.UTF8))
        {
            int readCount;
            char[] buffer = new char[1024];
            while ((readCount = sr.Read(buffer, 0, 1024)) != 0)
            {
                for (int i = 0; i < readCount; i++)
                {
                    Console.Write("{0} ", buffer[i]);
                }
            }
        }
    }
}




















Home »
  C# Tutorial »
    IO »




File Attribute
File Security
Directory Attribute
Directory Recursive
Binary File
Text Field
Buffered IO
Create Copy Delete Move
CSV
Drive
File System Watcher
Isolated Storage
MemoryStream
Serialize
Zip