FileStream.Read メソッド

定義

オーバーロード

Read(Byte[], Int32, Int32)

ストリームからバイトのブロックを読み取り、そのデータを特定のバッファーに書き込みます。

Read(Span<Byte>)

現在のファイル ストリームからバイト シーケンスを読み取り、読み取られたバイト数だけファイル ストリーム内の位置を進めます。

Read(Byte[], Int32, Int32)

ソース:
FileStream.cs
ソース:
FileStream.cs
ソース:
FileStream.cs

ストリームからバイトのブロックを読み取り、そのデータを特定のバッファーに書き込みます。

public:
 override int Read(cli::array <System::Byte> ^ array, int offset, int count);
public:
 override int Read(cli::array <System::Byte> ^ buffer, int offset, int count);
public override int Read (byte[] array, int offset, int count);
public override int Read (byte[] buffer, int offset, int count);
override this.Read : byte[] * int * int -> int
override this.Read : byte[] * int * int -> int
Public Overrides Function Read (array As Byte(), offset As Integer, count As Integer) As Integer
Public Overrides Function Read (buffer As Byte(), offset As Integer, count As Integer) As Integer

パラメーター

arraybuffer
Byte[]

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

offset
Int32

読み取られるバイトが配置される array 内のバイト オフセット。

count
Int32

読み取る最大バイト数。

戻り値

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

例外

arraynullです。

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

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

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

offsetcountarray の無効な範囲を示しています。

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

次の例では、 から内容を FileStream 読み取り、別 FileStreamの に書き込みます。

using System;
using System.IO;

class Test
{

public static void Main()
{
    // Specify a file to read from and to create.
    string pathSource = @"c:\tests\source.txt";
    string pathNew = @"c:\tests\newfile.txt";

    try
    {

        using (FileStream fsSource = new FileStream(pathSource,
            FileMode.Open, FileAccess.Read))
        {

            // Read the source file into a byte array.
            byte[] bytes = new byte[fsSource.Length];
            int numBytesToRead = (int)fsSource.Length;
            int numBytesRead = 0;
            while (numBytesToRead > 0)
            {
                // Read may return anything from 0 to numBytesToRead.
                int n = fsSource.Read(bytes, numBytesRead, numBytesToRead);

                // Break when the end of the file is reached.
                if (n == 0)
                    break;

                numBytesRead += n;
                numBytesToRead -= n;
            }
             numBytesToRead = bytes.Length;

            // Write the byte array to the other FileStream.
            using (FileStream fsNew = new FileStream(pathNew,
                FileMode.Create, FileAccess.Write))
            {
                fsNew.Write(bytes, 0, numBytesToRead);
            }
        }
    }
    catch (FileNotFoundException ioEx)
    {
        Console.WriteLine(ioEx.Message);
    }
}
}
open System.IO

// Specify a file to read from and to create.
let pathSource = @"c:\tests\source.txt"
let pathNew = @"c:\tests\newfile.txt"

try
    use fsSource = new FileStream(pathSource, FileMode.Open, FileAccess.Read)

    // Read the source file into a byte array.
    let mutable numBytesToRead = int fsSource.Length
    let bytes = numBytesToRead |> Array.zeroCreate
    let mutable numBytesRead = 0

    while numBytesToRead > 0 do
        // Read may return anything from 0 to numBytesToRead.
        let n = fsSource.Read(bytes, numBytesRead, numBytesToRead)

        // Break when the end of the file is reached.
        if n <> 0 then
            numBytesRead <- numBytesRead + n
            numBytesToRead <- numBytesToRead - n

    let numBytesToRead = bytes.Length

    // Write the byte array to the other FileStream.
    use fsNew = new FileStream(pathNew, FileMode.Create, FileAccess.Write)
    fsNew.Write(bytes, 0, numBytesToRead)
with :? FileNotFoundException as ioEx ->
    printfn $"{ioEx.Message}"
Imports System.IO
Class Test
    
Public Shared Sub Main()
    ' Specify a file to read from and to create.
    Dim pathSource As String = "c:\tests\source.txt"
    Dim pathNew As String = "c:\tests\newfile.txt"
    Try 
        Using fsSource As FileStream = New FileStream(pathSource, _
            FileMode.Open, FileAccess.Read)
            ' Read the source file into a byte array.
                Dim bytes() As Byte = New Byte((fsSource.Length) - 1) {}
                Dim numBytesToRead As Integer = CType(fsSource.Length,Integer)
                Dim numBytesRead As Integer = 0

                While (numBytesToRead > 0)
                    ' Read may return anything from 0 to numBytesToRead.
                    Dim n As Integer = fsSource.Read(bytes, numBytesRead, _
                        numBytesToRead)
                    ' Break when the end of the file is reached.
                    If (n = 0) Then
                        Exit While
                    End If
                    numBytesRead = (numBytesRead + n)
                    numBytesToRead = (numBytesToRead - n)

                End While
            numBytesToRead = bytes.Length

            ' Write the byte array to the other FileStream.
            Using fsNew As FileStream = New FileStream(pathNew, _
                FileMode.Create, FileAccess.Write)
                fsNew.Write(bytes, 0, numBytesToRead)
            End Using
        End Using
    Catch ioEx As FileNotFoundException
        Console.WriteLine(ioEx.Message)
    End Try
End Sub
End Class

注釈

このメソッドは、Read をオーバーライドします。

パラメーターは offset 、読み取りを開始するバイトの array オフセット (バッファー インデックス) を指定し count 、 パラメーターは、このストリームから読み取るバイトの最大数を指定します。 返される値は、読み取られた実際のバイト数です。ストリームの末尾に達した場合は 0 です。 読み取り操作が成功した場合、ストリームの現在位置は読み取られたバイト数だけ進みます。 例外が発生した場合、ストリームの現在位置は変更されません。

メソッドは Read 、ストリームの末尾に達した後にのみ 0 を返します。 それ以外の場合は、 Read 返される前に常にストリームから少なくとも 1 バイトを読み取ります。 への Read呼び出し時にストリームからデータが使用できない場合、メソッドは少なくとも 1 バイトのデータを返すまでブロックします。 実装は、ストリームの末尾に達していない場合でも、要求されたバイト数よりも少ないバイト数を自由に返します。

プリミティブ データ型の読み取りに使用 BinaryReader します。

読み取り操作を実行しているスレッドを中断しないでください。 スレッドのブロックが解除された後にアプリケーションが正常に実行されるように見えることがありますが、中断によってアプリケーションのパフォーマンスと信頼性が低下する可能性があります。

一般的なファイル操作とディレクトリ操作の一覧については、「 一般的な I/O タスク」を参照してください。

こちらもご覧ください

適用対象

Read(Span<Byte>)

ソース:
FileStream.cs
ソース:
FileStream.cs
ソース:
FileStream.cs

現在のファイル ストリームからバイト シーケンスを読み取り、読み取られたバイト数だけファイル ストリーム内の位置を進めます。

public:
 override int Read(Span<System::Byte> buffer);
public override int Read (Span<byte> buffer);
override this.Read : Span<byte> -> int
Public Overrides Function Read (buffer As Span(Of Byte)) As Integer

パラメーター

buffer
Span<Byte>

メモリの領域。 このメソッドが戻ると、この領域のコンテンツは現在のファイル ストリームから読み取られたバイトに置き換えられます。

戻り値

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

注釈

プロパティを使用して、 CanRead 現在のインスタンスが読み取りをサポートしているかどうかを判断します。 メソッドを ReadAsync 使用して、現在のストリームから非同期的に読み取ります。

このメソッドは、現在の buffer.Length ファイル ストリームから最大バイト数を読み取り、 に buffer格納します。 ファイル ストリーム内の現在の位置は、読み取られたバイト数だけ進みます。ただし、例外が発生した場合、ファイル ストリーム内の現在の位置は変更されません。 メソッドは、データが使用できない場合に、少なくとも 1 バイトのデータを読み取ることができるまでブロックします。 Read は、ファイル ストリームにこれ以上データがなく、それ以上必要ない場合 (閉じたソケットやファイルの終わりなど) にのみ 0 を返します。 メソッドは、ファイル ストリームの末尾に達していない場合でも、要求されたバイト数よりも少ないバイト数を自由に返します。

プリミティブ データ型の読み取りに使用 BinaryReader します。

適用対象