C# BinaryWriter and BinaryReader Example Program



Program

using System;
using System.IO;

namespace IO_Samples
{
    class BinaryWriterAndReader
    {
        public static void Main(String[] args)
        {
            Console.WriteLine("How many Records you want to store?");
            int recordCount = int.Parse(Console.ReadLine());

            BinaryWriter bw = null;
            //Creating a binary file
            try
            {
                bw = new BinaryWriter(new FileStream 
                 (@"C:\TrainerSNA\TraineeData.txt", FileMode.Create));
                for (int i = 1; i <= recordCount; i++)
                {
                    Console.WriteLine("Name?");//string
                    bw.Write(Console.ReadLine());

                    Console.WriteLine("Age?");//int
                    bw.Write(Convert.ToInt32(Console.ReadLine()));

                    Console.WriteLine("Height?");//float      
                    bw.Write(double.Parse(Console.ReadLine()));

                    Console.WriteLine("Employed? (Yes/No)");//Boolean
                    bw.Write(Console.ReadLine().ToLower().Equals("yes") ?                                                              true:false);
                }
            }
            catch (IOException e)
            {
                Console.WriteLine(e.Message + "\nCannot create or Write                                                                     file.");
            }
            finally
            {
                if (bw != null)
                {
                    bw.Close();
                }
            }

            BinaryReader br = null;
            //Reading a binary file
            try
            {
                br = new BinaryReader(new FileStream 
                (@"C:\TrainerSNA\TraineeData.txt", FileMode.Open));

                for (int i = 1; i <= recordCount; i++)
                {
                string nm = br.ReadString();
                Console.WriteLine("Name     :{0}", nm);

                Int32 age = br.ReadInt32();
                Console.WriteLine("Age      :{0}", age);

                double hgt = br.ReadDouble();
                Console.WriteLine("height   :{0}", hgt);

                bool mstatus = br.ReadBoolean();
                Console.WriteLine("Employment Status:{0}", mstatus ?                                                  "Employed" : "Un-Employed");
                }
            }
            catch (IOException e)
            {
                Console.WriteLine(e.Message + "\nCannot open or read                                                                    file");
            }
            finally
            {
                if (bw != null)
                {
                    bw.Close();
                }
            }

            Console.ReadKey();
        }
    }
}

Output

How many Records you want to store?
2
Name?
SYED
Age?
35
Height?
5.11
Employed? (Yes/No)
YES
Name?
ANTONY
Age?
29
Height?
6.1
Employed? (Yes/No)
NO
Name     SYED
Age      :35
height   :5.11
Employment Status: Employed
Name     ANTONY
Age      :29
height   :6.1
Employment Status: Un-Employed

No comments:

Post a Comment