FileStream.Read 方法

从流中读取字节块并将该数据写入给定缓冲区中。

**命名空间:**System.IO
**程序集:**mscorlib(在 mscorlib.dll 中)

语法

声明
Public Overrides Function Read ( _
    <InAttribute> <OutAttribute> array As Byte(), _
    offset As Integer, _
    count As Integer _
) As Integer
用法
Dim instance As FileStream
Dim array As Byte()
Dim offset As Integer
Dim count As Integer
Dim returnValue As Integer

returnValue = instance.Read(array, offset, count)
public override int Read (
    [InAttribute] [OutAttribute] byte[] array,
    int offset,
    int count
)
public:
virtual int Read (
    [InAttribute] [OutAttribute] array<unsigned char>^ array, 
    int offset, 
    int count
) override
public int Read (
    /** @attribute InAttribute() */ /** @attribute OutAttribute() */ byte[] array, 
    int offset, 
    int count
)
public override function Read (
    array : byte[], 
    offset : int, 
    count : int
) : int

参数

  • array
    当此方法返回时,包含指定的数组,数组中 offset 和 (offset + count - 1) 之间的值被从当前源中读取的字节替换。
  • offset
    array 中的字节偏移量,从此处开始读取。
  • count
    最多读取的字节数。

返回值

读入缓冲区中的总字节数。如果当前的字节数没有所请求那么多,则总字节数可能小于所请求的字节数;或者如果已到达流的末尾,则为零。

异常

异常类型 条件

ArgumentNullException

array 为 空引用(在 Visual Basic 中为 Nothing)。

ArgumentOutOfRangeException

offset 或 count 为负。

NotSupportedException

流不支持读取。

IOException

发生 I/O 错误。

ArgumentException

offset 和 count 描述 array 中的无效范围。

ObjectDisposedException

在流关闭后调用方法。

备注

此方法重写 Read

offset 参数给出 array 中字节的偏移量(缓冲区索引),从此处开始读取,count 参数给出从此流最多读取的字节数。返回的值是读取字节的实际数量,或如果到达流的结尾,则该值为零。如果读操作成功,则流的当前位置前进读取的字节数。如果发生异常,则流的当前位置不变。

只有在到达流的末尾后,Read 方法才返回零。否则,Read 在返回前始终至少从流读取一个字节。如果在调用 Read 之后流中无可用数据,则该方法将一直阻止,直到至少可返回一个字节的数据。即使尚未到达流的末尾,实现仍可以随意返回少于所请求的字节。

BinaryReader 用于读取基元数据类型。

下表列出了其他典型或相关的 I/O 任务的示例。

若要执行此操作...

请参见本主题中的示例...

创建文本文件。

如何:向文件写入文本

写入文本文件。

如何:向文件写入文本

读取文本文件。

如何:从文件读取文本

向文件中追加文本。

如何:打开并追加到日志文件

File.AppendText

FileInfo.AppendText

重命名或移动文件。

File.Move

FileInfo.MoveTo

复制文件。

File.Copy

FileInfo.CopyTo

获取文件大小。

FileInfo.Length

获取文件属性。

File.GetAttributes

设置文件属性。

File.SetAttributes

确定文件是否存在。

File.Exists

读取二进制文件。

如何:对新建的数据文件进行读取和写入

写入二进制文件。

如何:对新建的数据文件进行读取和写入

创建目录。

Directory.CreateDirectory

Directory.CreateDirectory

示例

下面的示例说明如何从现有文件中读取指定的字节数。

Imports System
Imports System.IO

Class FSRead

    Public Shared Sub Main()
        'Create a file stream from an existing file.
        Dim fi As New FileInfo("c:\csc.txt")
        Dim fs As FileStream = fi.OpenRead()

        'Read 100 bytes into an array from the specified file.
        Dim nBytes As Integer = 100
        Dim ByteArray(nBytes) As Byte
        Dim nBytesRead As Integer = fs.Read(ByteArray, 0, nBytes)
        Console.WriteLine("{0} bytes have been read from the specified file.", nBytesRead.ToString())
    End Sub 'Main
End Class 'FSRead
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());
    }
}
using namespace System;
using namespace System::IO;
int main()
{
   
   //Create a file stream from an existing file.
   FileInfo^ fi = gcnew FileInfo( "c:\\csc.txt" );
   FileStream^ fs = fi->OpenRead();
   
   //Read 100 bytes into an array from the specified file.
   int nBytes = 100;
   array<Byte>^ByteArray = gcnew array<Byte>(nBytes);
   int nBytesRead = fs->Read( ByteArray, 0, nBytes );
   Console::WriteLine( "{0} bytes have been read from the specified file.", nBytesRead );
}
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

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0、1.0

请参见

参考

FileStream 类
FileStream 成员
System.IO 命名空间

其他资源

文件和流 I/O
如何:从文件读取文本
如何:向文件写入文本