Stream.Read メソッドとは? わかりやすく解説

Stream.Read メソッド

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

名前空間: System.IO
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

Public MustOverride Function
 Read ( _
    <InAttribute> <OutAttribute> buffer As Byte(),
 _
    offset As Integer, _
    count As Integer _
) As Integer
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
)

パラメータ

buffer

バイト配列。このメソッドが戻るとき、指定したバイト配列offset から (offset + count -1) までの値が、現在のソースから読み取られたバイト置き換えられます。

offset

現在のストリームから読み取ったデータ格納開始する位置を示す buffer内のバイト オフセットインデックス番号は 0 から始まります

count

現在のストリームから読み取る最大バイト数。

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

例外例外
例外種類条件

ArgumentException

offsetcount合計値が、バッファより大きい値です。

ArgumentNullException

buffernull 参照 (Visual Basic では Nothing) です。

ArgumentOutOfRangeException

offset または count が負の値です。

IOException

I/O エラー発生しました

NotSupportedException

ストリーム読み取りサポートしていません。

ObjectDisposedException

ストリーム閉じられ後でメソッド呼び出されました。

解説解説

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

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

このメソッド実装は、現在のストリームから count指定した最大バイト数だけ読み込み読み込んだバイトbuffer 内の offset で始まる位置格納しますストリーム現在位置読み込んだバイト数だけ進みます。ただし、例外発生した場合は、ストリーム内の現在位置そのまま変わりません。読み込んだバイト数が返されます。現在位置ストリーム末尾にある場合戻り値は 0 です。読み込み可能なデータない場合は、少なくとも 1 バイトデータ読み込み可能になるまでブロックします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 );
}

プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照


このページでは「.NET Framework クラス ライブラリ リファレンス」からStream.Read メソッドを検索した結果を表示しています。
Weblioに収録されているすべての辞書からStream.Read メソッドを検索する場合は、下記のリンクをクリックしてください。
 全ての辞書からStream.Read メソッドを検索

英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

「Stream.Read メソッド」の関連用語

Stream.Read メソッドのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



Stream.Read メソッドのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2024 Microsoft.All rights reserved.

©2024 GRAS Group, Inc.RSS