Write encoded byte array to text file in CSharp

Description

The following code shows how to write encoded byte array to text file.

Example


using System;/* ww w.  j  a  v a2 s . com*/
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

    class Program
    {
        static void Main(string[] args)
        {
            FileStream aFile = new FileStream("Temp.txt", FileMode.Create);
            char[] charData = "this is a test.".ToCharArray();
            byte[] byData = new byte[charData.Length];
            Encoder e = Encoding.UTF8.GetEncoder();
            e.GetBytes(charData, 0, charData.Length, byData, 0, true);

            // Move file pointer to beginning of file.
            aFile.Seek(0, SeekOrigin.Begin);
            aFile.Write(byData, 0, byData.Length);
        }
    }




















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