C# FileStream Read
Description
FileStream Read
Reads a block of bytes from the stream
and writes the data in a given buffer.
Syntax
FileStream.Read
has the following syntax.
public override int Read(
byte[] array,// w w w . ja va 2 s .c o m
int offset,
int count
)
Parameters
FileStream.Read
has the following parameters.
array
- When this method returns, contains the specified byte array with the values between offset and (offset + count - 1) replaced by the bytes read from the current source.offset
- The byte offset in array at which the read bytes will be placed.count
- The maximum number of bytes to read.
Returns
FileStream.Read
method returns The total number of bytes read into the buffer. This might be less than the
number of bytes requested if that number of bytes are not currently available,
or zero if the end of the stream is reached.
Example
The following example reads the contents from a FileStream and writes it into another FileStream.
/* ww w. j a va 2 s .co m*/
using System;
using System.IO;
class Test
{
public static void Main()
{
string pathSource = @"c:\tests\source.txt";
string pathNew = @"c:\tests\newfile.txt";
using (FileStream fsSource = new FileStream(pathSource, FileMode.Open, FileAccess.Read))
{
byte[] bytes = new byte[fsSource.Length];
int numBytesToRead = (int)fsSource.Length;
while (numBytesToRead > 0)
{
int n = fsSource.Read(bytes, 3, numBytesToRead);
Console.WriteLine(bytes.Length);
if (n == 0)
break;
}
}
}
}