Sample program to know use of Binary Reader and writer

Manimaddu
Posted by Manimaddu under C# category on | Points: 40 | Views : 492
Hi All..
The below code explains the use of binary reader and binary writer.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace UseOfBinaryReaderAndwriter
{
class Program
{
static void Main(string[] args)
{
BinaryWriter bwObj;
BinaryReader brObj;
int i = 30;
double d = 4.567;
bool b = true;
string s = "GudMorning Prime";
try
{
bwObj=new BinaryWriter(new FileStream("Manikumar", FileMode.Create));
}
catch (IOException e)
{
Console.WriteLine(e.Message + "\n File Cannot Create..");
return;
}
try
{
bwObj.Write(i);
bwObj.Write(d);
bwObj.Write(b);
bwObj.Write(s);
}
catch(IOException e)
{
Console.WriteLine(e.Message + "\nCan not write to file");
return;
}
bwObj.Close();
try
{
brObj = new BinaryReader(new FileStream("manikumar", FileMode.Open));
}
catch (IOException e)
{
Console.WriteLine(e.Message + "\n Cannot open the file");
return;
}
try
{
i = brObj.ReadInt32();
Console.WriteLine("Integer Data = {0}", i);
d = brObj.ReadDouble();
Console.WriteLine("Double Data = {0}", d);
b = brObj.ReadBoolean();
Console.WriteLine("Boolean Data = {0}", b);
s = brObj.ReadString();
Console.WriteLine("String is = {0}", s);
}
catch (IOException e)
{
Console.WriteLine(e.Message + "\ncannot read the file");
return;
}
brObj.Close();
Console.ReadKey();
}
}
}



Thank You...

Comments or Responses

Login to post response