Read text file into byte array, and write byte array to another file in CSharp

Description

The following code shows how to read text file into byte array, and write byte array to another file.

Example


         //from w ww  . j  a  va 2s.  c  o m

using System;
using System.Windows.Forms;
using System.IO;

class MainClass
{
  public static void Main() 
  {
    FileStream inStream = File.OpenRead("C:\\test.txt");
    FileStream outStream = File.OpenWrite("C:\\test.txt" + ".bak");
    byte[] buf = new byte[4096];
    int bytesRead;

    while ((bytesRead = inStream.Read(buf, 0, 4096)) > 0)
      outStream.Write(buf, 0, bytesRead);

    outStream.Flush();
    outStream.Close();
    inStream.Close();

  }
}




















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