次の方法で共有


Stream.Read メソッド

派生クラスによってオーバーライドされた場合は、現在のストリームからバイト シーケンスを読み取り、読み取ったバイト数の分だけストリームの位置を進めます。

Public MustOverride Function Read( _
   <InteropServices.In(), _
   Out()> ByVal buffer() As Byte, _   ByVal offset As Integer, _   ByVal count As Integer _) As Integer
[C#]
public abstract int Read(   [   In,   Out] byte[] buffer,intoffset,intcount);
[C++]
public: virtual int Read(   [   In,   Out] unsigned charbuffer __gc[],intoffset,intcount) = 0;
[JScript]
public abstract function Read(
   buffer : Byte[],offset : int,count : int) : int;

パラメータ

  • buffer
    バイト配列。このメソッドが戻るとき、指定したバイト配列の offset から (offset + count-1) までの値が、現在のソースから読み取られたバイトに置き換えられます。
  • offset
    現在のストリームから読み取ったデータの格納を開始する位置を示す buffer 内のバイト オフセット。インデックス番号は 0 から始まります。
  • count
    現在のストリームから読み取る最大バイト数。

戻り値

バッファに読み取られた合計文字数。要求しただけのバイト数を読み取ることができなかった場合、この値は要求したバイト数より小さくなります。ストリームの末尾に到達した場合は 0 (ゼロ) になることがあります。

例外

例外の種類 条件
ArgumentException offsetcount の合計値が、バッファ長より大きい値です。
ArgumentNullException buffer が null 参照 (Visual Basic では Nothing) です。
ArgumentOutOfRangeException offset または count が負の値です。
IOException I/O エラーが発生しました。
NotSupportedException ストリームが読み取りをサポートしていません。
ObjectDisposedException ストリームが閉じられた後でメソッドが呼び出されました。

解説

ファイルの作成およびテキストのファイルへの書き込みの例については、「 ファイルへのテキストの書き込み 」を参照してください。ファイルからのテキストの読み取りの例については、「 ファイルからのテキストの読み取り 」を参照してください。バイナリ ファイルの読み取りおよび書き込みの例については、「 新しく作成したデータ ファイルの読み取りと書き込み 」を参照してください。

現在のインスタンスが読み取りをサポートしているかどうかを判断するには、 CanRead プロパティを使用します。

このメソッドの実装は、現在のストリームから count で指定した最大バイト数だけ読み込み、読み込んだバイトを buffer 内の offset で始まる位置に格納します。ストリームの現在位置が読み込んだバイト数だけ進みます。ただし、例外が発生した場合は、ストリーム内の現在位置はそのまま変わりません。読み込んだバイト数が返されます。現在位置がストリームの末尾にある場合、戻り値は 0 です。読み込み可能なデータがない場合は、少なくとも 1 バイトのデータが読み込み可能になるまでブロックします。 Read が 0 を返すのは、ストリーム内にそれ以上データがない場合とそれ以上読み込み可能な対象 (閉じたソケットやファイル終端など) を予期していない場合だけです。メソッドの実装は、ストリームの末尾に到達していない場合でも、要求された数に満たないバイトを返すようにすることができます。

プリミティブ データ型を読み取る場合は BinaryReader を使用します。

使用例

[Visual Basic, C#, C++] 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(1000) 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

[C#] 
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[1000];
        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);
    }
}

[C++] 
#using <mscorlib.dll>
using namespace System;
using namespace System::IO;

int 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[1000];
    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(S"number of bytes read: {0}", __box(numBytesRead));
}

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET, Common Language Infrastructure (CLI) Standard

参照

Stream クラス | Stream メンバ | System.IO 名前空間 | 入出力操作 | ファイルからのテキストの読み取り | ファイルへのテキストの書き込み