FileStream.Read 方法

定义

重载

Read(Byte[], Int32, Int32)

从流中读取字节块并将该数据写入给定缓冲区中。

Read(Span<Byte>)

从当前文件流中读取字节序列,并在该文件流中按照读取的字节数提升位置。

Read(Byte[], Int32, Int32)

Source:
FileStream.cs
Source:
FileStream.cs
Source:
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>)

Source:
FileStream.cs
Source:
FileStream.cs
Source:
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 读取基元数据类型。

适用于