Monday, 5 March 2018

027 C# File Handleing Part 2

MemoryStream : A sequence of array off byte  to read and write operation of file. The syntax is Memory stream(byte[] buffer). The arrayed buffer  we flow from  source to target file sequentially . This technique is useful for GUI  programming, buffer is retain  till it is needed.


Example 
using System;
using System.IO;
class Program
  {
    static void Main()
     {
          string file_path = "C:\\ab\\test.txt";
         string new_file_path = "C:\\ab\\test2.txt";
          
      //Example Read file in Memory Stream
         using (FileStream fs = File.OpenRead(file_path))
          {
               MemoryStream ms = new MemoryStream();
               ms.SetLength(fs.Length);
              fs.Read(ms.GetBuffer(), 0, (int)fs.Length);
        }

       //Example Write file in Using Memory Stream
       using (FileStream fs = File.OpenRead(file_path))
       {
               MemoryStream ms = new MemoryStream();
               ms.SetLength(fs.Length);
              fs.Read(ms.GetBuffer(), 0, (int)fs.Length);
              FileStream new_fs = new FileStream(new_file_path, FileMode.Create, FileAccess.Write);
              ms.WriteTo(new_fs);
             new_fs.Close();
       }
     Console.ReadKey();
    }
}


Reading from and Writing to Text Files     To read text file .Net Framework StreamReader and StreamWriter class is used. Both StreamReader and StreamWriter class comes under the System.IO namespace.

StreamReader Class :It is a class which read character from a byte stream. It is encoded with a particular encoding system. Generally UTF- 8  encoding is used for encoding, it is a default encoding also. StreamReader have several method to read stream of character from a standard text file. Bellow is the example of some methods.


  •   StreamReader.Read : Read the next character from input stream and advanced by one  character.
  •   StreamReader.ReadToEnd : Read all from start to end.
  •   StreamReader.ReadLine : Read entire line from a input Stream.
  •   StreamReader.ReadBlock : Read Specific nummber of character from input stream and buffered.
  •   StreamReader.EndOfStream : Indicator that the Stream has ended or not.
  •   StreamReader.Close : Close StreamReader and release all resource.
  •   StreamReader.Dispose : Releaes all resource.

 Example 

using System; 

using System.IO;

class Program
{
          static void Main()
          {
                string file_path = "C:\\ab\\test.txt"; 

               //Read by Characted by Character
               using (StreamReader reader = new StreamReader(file_path))
                {
                      while(reader.Read()>0)
                     {
                     }
               }
 
        //Read by Line by Line
            using (StreamReader reader = new StreamReader(file_path))
           {
                 while (reader.ReadLine()!= null)
                  {
                  }
          }

         //Read by ReadBlock
        using (StreamReader reader = new StreamReader(file_path))
        {
           char[] ch = new char[3];
          reader.ReadBlock(ch, 0, 3);
       }

        //Read till end of File
       StreamReader reader1 = new StreamReader(file_path);
         while (!(reader1 as StreamReader).EndOfStream)
         {
        }
        Console.ReadKey();
     }
}

StreamWriter  Class : StreamWriter write character to a stream with a particular encoding. The default encoding UTF-8. StreamWriter have several method to write character in a stream. Below is the example of some method.

StreamWriter.Write : White character from Stream.

StreamWriter.WriteLine : Write a line.

StreamWriter.Flush : Clear buffer.

StreamWriter.NewLine :Write a line terminator character.

StreamWriter.Close : Close StreamReader and release all resource.

StreamWriter.Dispose : Releaes all resource.
Example 

using System;

using System.IO;

class Program
{
        static void Main()
        {
            string file_path = "C:\\ab\\test.txt";

           //StreamWriter Write Example
            char[] arr = new char[] { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o' };

            using (StreamWriter sw = File.CreateText(file_path))
           {
                 for (int i = 1; i <= 10; i++)
                 {
                   sw.Write(arr[i]);
                }
          }
//StreamWriter WriteLine Example
           using (StreamWriter sw = File.CreateText(file_path))
            {
              for (int i = 1; i <= 10; i++)
              {
                 sw.WriteLine("Hellow World 1");
               }
         }

         Console.ReadKey();
     }
}
Reading from and Writing into Binary files


For the purpose of writing and reading binary file binary reader and binary writer classes are used. Binary reader the primitive data from binary files form a stream. A binary file may be thousands of integer value simple data type. BinaryReader also used to read with specific in coding system. 
BinaryWriter classes is for write primitive data as binary files with a specific in coding system.
 

Example : Example of BinaryReader
using System;
using System.IO;
    class Program
    {
        static void Main()
        {
               string file_path = "C:\\ab\\test.dll";
               FileStream fs = File.OpenRead(file_path);
              BinaryReader b = new BinaryReader(fs);
             string msg = b.ReadString();
             b.Close();
            Console.ReadKey();
      }
}

Example : Example of BinaryWriter

using System;
using System.IO;
   class Program
   {
      static void Main()
     {
        string file_path = "C:\\ab\\test.dll";
        using (BinaryWriter writer = new BinaryWriter(File.Open(file_path, FileMode.Create)))
       {
          writer.Write("Error Writing File");
       }
       Console.ReadKey();
   }
}





No comments:

Post a Comment

বাঙালির বেড়ানো সেরা চারটি ঠিকানা

  বাঙালি মানে ঘোড়া পাগল | দু একদিন ছুটি পেলো মানে বাঙালি চলল ঘুরতে | সে সমুদ্রই হোক , পাহাড়ি হোক বা নদী হোক। বাঙালির ...