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

要讀取的最大位元組數。

傳回

緩衝區所讀取的總位元組數。 如果目前無法提供那麼多的位元組數目,則這個數目可能小於所要求的位元組數;如果已經到達資料流末端,則為零。

例外狀況

arraynull

offsetcount 為負。

資料流不支援讀取。

發生 I/O 錯誤。

offsetcount 描述 array 中的無效範圍。

關閉資料流後呼叫了方法。

範例

下列範例會從 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 參數會提供要從此資料流程讀取的最大位元組數目。 傳回的值是讀取的實際位元組數,如果到達資料流程結尾,則為零。 如果讀取作業成功,資料流程的目前位置會由讀取的位元組數目進階。 如果發生例外狀況,資料流程的目前位置不會變更。

方法 Read 只有在到達資料流程結尾之後,才會傳回零。 否則,在傳回之前, Read 一律從資料流程讀取至少一個位元組。 如果在呼叫 Read 時無法從資料流程取得任何資料,方法將會封鎖,直到可以傳回至少一個位元組的資料為止。 即使尚未到達資料流程結尾,實作仍可傳回比要求較少的位元組。

用於 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 中。 檔案資料流程內的目前位置會依讀取的位元組數目進階;不過,如果發生例外狀況,檔案資料流程內的目前位置會保持不變。 方法會封鎖,直到可以讀取至少一個位元組的資料,否則不會有資料可供使用。 Read 只有在檔案資料流程中沒有其他資料,而且不會再傳回 0,而且不會再傳回 (,例如關閉的通訊端或檔案結尾) 。 即使尚未到達檔案資料流程結尾,方法仍可傳回比要求較少的位元組。

用於 BinaryReader 讀取基本資料類型。

適用於