FileStream.Read Method
Assembly: mscorlib (in mscorlib.dll)
public override int Read ( [InAttribute] [OutAttribute] byte[] array, int offset, int count )
public int Read ( /** @attribute InAttribute() */ /** @attribute OutAttribute() */ byte[] array, int offset, int count )
public override function Read ( array : byte[], offset : int, count : int ) : int
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 to begin reading.
- count
The maximum number of bytes to read.
Return Value
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.This method overrides Read.
The offset parameter gives the offset of the byte in array (the buffer index) at which to begin reading, and the count parameter gives the maximum number of bytes to be read from this stream. The returned value is the actual number of bytes read, or zero if the end of the stream is reached. If the read operation is successful, the current position of the stream is advanced by the number of bytes read. If an exception occurs, the current position of the stream is unchanged.
The Read method returns zero only after reaching the end of the stream. Otherwise, Read always reads at least one byte from the stream before returning. If no data is available from the stream upon a call to Read, the method will block until at least one byte of data can be returned. An implementation is free to return fewer bytes than requested even if the end of the stream has not been reached.
Use BinaryReader for reading primitive data types.
The following table lists examples of other typical or related I/O tasks.
| To do this... | See the example in this topic... |
|---|---|
| Create a text file. | |
| Write to a text file. | |
| Read from a text file. | |
| Append text to a file. | |
| Rename or move a file. | |
| Copy a file. | |
| Get the size of a file. | |
| Get the attributes of a file. | |
| Set the attributes of a file. | |
| Determine if a file exists. | |
| Read from a binary file. | |
| Write to a binary file. | |
| Create a directory. | Directory.CreateDirectory |
The following example demonstrates reading a specified number of bytes from an existing file.
using System; using System.IO; class FSRead { public static void Main() { //Create a file stream from an existing file. FileInfo fi=new FileInfo("c:\\csc.txt"); FileStream fs=fi.OpenRead(); //Read 100 bytes into an array from the specified file. int nBytes=100; byte[] ByteArray=new byte[nBytes]; int nBytesRead=fs.Read(ByteArray, 0, nBytes); Console.WriteLine("{0} bytes have been read from the specified file.", nBytesRead.ToString()); } }
import System.*;
import System.IO.*;
class FSRead
{
public static void main(String[] args)
{
//Create a file stream from an existing file.
FileInfo fi = new FileInfo("c:\\csc.txt");
FileStream fs = fi.OpenRead();
//Read 100 bytes into an array from the specified file.
int nBytes = 100;
ubyte byteArray[] = new ubyte[nBytes];
int nBytesRead = fs.Read(byteArray, 0, nBytes);
Console.WriteLine("{0} bytes have been read from the specified file.",
((Int32)nBytesRead).ToString());
} //main
} //FSRead
Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.