|
File access in .NET is generally handled with stream objects. In this section, we will discuss about the two classes BinaryWriter and BinaryReader to demonstrate the file handling in C#. The BinaryReader and BinaryWriter classes support binary file access. These classes permit binary operations to and from streams.
BinaryWriter
The BinaryWriter class allows primitive data types to be written to streams; and with the use of subclassing, you can override the methods of this class and fulfill your own unique character encoding requirements. The following table will summarize the properties of the class:-
Name
|
Type
|
Purpose
|
BaseStream
|
Property
|
Allow access to binary writer underlying stream.
|
Close
|
Method
|
Close the Binary writer class and the underlying stream.
|
Flush
|
Method
|
Flushes out all data to the underlying stream and then clears buffers.
|
Seek
|
Method
|
Sets the current position within the current stream.
|
Write
|
Method
|
This overloaded method writes a value out to the current stream.
|
BinaryReader
The BinaryReader class, much like the BinaryWriter class, relies on a FileStream object to read files .
Method
|
|
Description
|
Close
|
|
Close the binary reader object and the base stream.
|
PeekChar
|
|
Reads the next available byte from the stream but does not advance the byte or character position within the file.
|
ReadBoolean
|
|
Reads a Boolean [True/False] value from the stream.
|
ReadByte
|
|
Reads a single byte from the stream. There is also a readbytes method for specifying the number of bytes to read.
|
ReadChar
|
|
Reads a single char value from the stream. There is also a readchars method for specifying the number of chars to read.
|
ReadInt16
|
|
Reads an integer value (2 bytes).
|
ReadInt32
|
|
Reads a long value (4 bytes).
|
ReadInt64
|
|
Reads an eight-byte signed integer.
|
Example: Demonstrate working with files
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace _CSharpApplication
{
public partial class Form4 : Form
{
public Form4()
{
InitializeComponent();
}
private void button1_Click( object sender, EventArgs e)
{
FileStream myFStream = new FileStream ( "G:\\2CSharpApplication\\Test.txt" , FileMode .OpenOrCreate, FileAccess .ReadWrite);
BinaryWriter binWrit = new BinaryWriter (myFStream);
string testString = "Writing into a File..This is a test Value." ;
binWrit.Write(testString);
binWrit.Close();
myFStream.Close();
}
private void button2_Click( object sender, EventArgs e)
{
long imageWidth = 0;
long imageHeight = 0;
int imagePlanes = 0;
int imageBitCount = 0;
string [] cma = Environment .GetCommandLineArgs();
if (cma.GetUpperBound(0) >= 1)
{
FileStream myFStream = new FileStream (cma[1], FileMode .Open, FileAccess .Read);
BinaryReader binRead = new BinaryReader (myFStream);
binRead.BaseStream.Position=0x12;
imageWidth = binRead.ReadInt32();
imageHeight= binRead.ReadInt32();
imagesPlanes= binRead.ReadInt16();
imagesBitCount = binRead.ReadInt16();
Console .WriteLine( "[{0}] {1}x{2} {3}- bit" ,cma[1], imageWidth, imageHeight, imageBitCount);
binRead.Close();
myFStream.Close();
}
}
private void button3_Click( object sender, EventArgs e)
{
string [] strcla = Environment .GetCommandLineArgs();
if (strcla.GetUpperBound(0) == 2)
{
FileInfo cpyfi = new FileInfo (strcla[1]);
cpyfi.CopyTo(strcla[2], true );
MessageBox .Show( "Copied " + cpyfi.Length + " bytes." );
}
else
MessageBox .Show( "Usage: cp <input file> <output file>" );
}
private void button4_Click( object sender, EventArgs e)
{
string [] strcla = Environment .GetCommandLineArgs();
if (strcla.GetUpperBound(0) == 1)
{
FileInfo delfi = new FileInfo (strcla[1]);
delfi.Delete();
MessageBox .Show( "File : " + strcla[1]);
MessageBox .Show( "Attributes: " + delfi.Attributes.ToString());
MessageBox .Show( "File Deleted..." );
}
else
MessageBox .Show( "Usage: rm <filename>" );
}
private void button5_Click( object sender, EventArgs e)
{
string [] strcla = Environment .GetCommandLineArgs();
if (strcla.GetUpperBound(0) == 2)
{
FileInfo movfi = new FileInfo (strcla[1]);
movfi.MoveTo(strcla[2]);
MessageBox .Show( "File Created : " +movfi.CreationTime.ToString());
MessageBox .Show( "Moved to : " + strcla[2]);
}
else
MessageBox .Show( "Usage: mv <source file> <destination file>" );
}
}
} |