Stream.Read 方法

当在派生类中重写时,从当前流读取字节序列,并将此流中的位置提升读取的字节数。

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

语法

声明
Public MustOverride Function Read ( _
    <InAttribute> <OutAttribute> buffer As Byte(), _
    offset As Integer, _
    count As Integer _
) As Integer
用法
Dim instance As Stream
Dim buffer As Byte()
Dim offset As Integer
Dim count As Integer
Dim returnValue As Integer

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

参数

  • buffer
    字节数组。此方法返回时,该缓冲区包含指定的字符数组,该数组的 offset 和 (offset + count -1) 之间的值由从当前源中读取的字节替换。
  • offset
    buffer 中的从零开始的字节偏移量,从此处开始存储从当前流中读取的数据。
  • count
    要从当前流中最多读取的字节数。

返回值

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

异常

异常类型 条件

ArgumentException

offset 与 count 的和大于缓冲区长度。

ArgumentNullException

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

ArgumentOutOfRangeException

offset 或 count 为负。

IOException

发生 I/O 错误。

NotSupportedException

流不支持读取。

ObjectDisposedException

在流关闭后调用方法。

备注

有关创建文件和向文件中写入文本的示例,请参见 如何:向文件写入文本。有关从文件中读取文本的示例,请参见 如何:从文件读取文本。有关读取和写入二进制文件的示例,请参见 如何:对新建的数据文件进行读取和写入

使用 CanRead 属性可确定当前实例是否支持读取。

此方法的实现从当前流中读取最多的 count 个字节,并将它们存储在从 offset 开始的 buffer 中。流中的当前位置提升已读取的字节数;但是,如果出现异常,流中的当前位置保持不变。实现返回已读取的字节数。仅当位置当前位于流的末尾时,返回值才为零。如果没有任何可用的数据,该实现将一直阻塞到至少有一个字节的数据可读为止。仅当流中不再有其他的数据,而且也不再需要更多的数据(如已关闭的套接字或文件尾)时,Read 才返回 0。即使尚未到达流的末尾,实现仍可以随意返回少于所请求的字节。

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

示例

下面的示例显示如何使用 Read 读取数据块。

Imports System
Imports System.IO
Imports Microsoft.VisualBasic

Public Class Block

    Public Shared Sub Main()
        Dim s As New MemoryStream()
        Dim i As Integer
        For i = 0 To 99
            s.WriteByte(CByte(i))
        Next i
        s.Position = 0

        ' Now read in s into a byte buffer.
        Dim bytes(s.Length) As Byte
        Dim numBytesToRead As Integer = CInt(s.Length)
        Dim numBytesRead As Integer = 0
        While numBytesToRead > 0
            ' Read can return anything from 0 to numBytesToRead.
            Dim n As Integer = s.Read(bytes, numBytesRead, numBytesToRead)
            ' The end of the file has been reached.
            If n = 0 Then
                Exit While
            End If
            numBytesRead += n
            numBytesToRead -= n
        End While
        s.Close()
        ' numBytesToRead should be 0 now, and numBytesRead should
        ' equal 100.
        Console.WriteLine("number of bytes read: " & numBytesRead.ToString())
    End Sub
End Class
using System;
using System.IO;

public class Block
{
    public static void Main()
    {
        Stream s = new MemoryStream();
        for (int i=0; i<100; i++)
            s.WriteByte((byte)i);
        s.Position = 0;
 
        // Now read s into a byte buffer.
        byte[] bytes = new byte[s.Length];
        int numBytesToRead = (int) s.Length;
        int numBytesRead = 0;
        while (numBytesToRead > 0) 
        {
            // Read may return anything from 0 to numBytesToRead.
            int n = s.Read(bytes, numBytesRead, numBytesToRead);
            // The end of the file is reached.
            if (n==0)
                break;
            numBytesRead += n;
            numBytesToRead -= n;
        }
        s.Close();
        // numBytesToRead should be 0 now, and numBytesRead should
        // equal 100.
        Console.WriteLine("number of bytes read: "+numBytesRead);
    }
}
using namespace System;
using namespace System::IO;
int main()
{
   Stream^ s = gcnew MemoryStream;
   for ( int i = 0; i < 100; i++ )
      s->WriteByte( (Byte)i );
   s->Position = 0;
   
   // Now read s into a byte buffer.
   array<Byte>^bytes = gcnew array<Byte>(s->Length);
   int numBytesToRead = (int)s->Length;
   int numBytesRead = 0;
   while ( numBytesToRead > 0 )
   {
      
      // Read may return anything from 0 to numBytesToRead.
      int n = s->Read( bytes, numBytesRead, numBytesToRead );
      
      // The end of the file is reached.
      if ( n == 0 )
            break;

      numBytesRead += n;
      numBytesToRead -= n;
   }

   s->Close();
   
   // numBytesToRead should be 0 now, and numBytesRead should
   // equal 100.
   Console::WriteLine( "number of bytes read: {0}", numBytesRead );
}

平台

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

请参见

参考

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

其他资源

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