FileStream.Read FileStream.Read FileStream.Read FileStream.Read Method

定义

重载

Read(Byte[], Int32, Int32) Read(Byte[], Int32, Int32) Read(Byte[], Int32, Int32) Read(Byte[], Int32, Int32)

从流中读取字节块并将该数据写入给定缓冲区中。Reads a block of bytes from the stream and writes the data in a given buffer.

Read(Span<Byte>) Read(Span<Byte>) Read(Span<Byte>) Read(Span<Byte>)

Read(Byte[], Int32, Int32) Read(Byte[], Int32, Int32) Read(Byte[], Int32, Int32) Read(Byte[], Int32, Int32)

从流中读取字节块并将该数据写入给定缓冲区中。Reads a block of bytes from the stream and writes the data in a given buffer.

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

参数

array
Byte[]

当此方法返回时,包含指定的字节数组,此数组中 offset 和 (offset + count - 1) 之间的值被从当前源中读取的字节所替换。When this method returns, contains the specified byte array with the values between offset and (offset + count - 1) replaced by the bytes read from the current source.

offset
Int32 Int32 Int32 Int32

array 中的字节偏移量,将在此处放置读取的字节。The byte offset in array at which the read bytes will be placed.

count
Int32 Int32 Int32 Int32

最多读取的字节数。The maximum number of bytes to read.

返回

读入缓冲区中的总字节数。The total number of bytes read into the buffer. 如果字节数当前不可用,则总字节数可能小于所请求的字节数;如果已到达流结尾,则为零。This might be less than the number of bytes requested if that number of bytes are not currently available, or zero if the end of the stream is reached.

异常

流不支持读取。The stream does not support reading.

出现 I/O 错误。An I/O error occurred.

offsetcount 描述 array 中的无效范围。offset and count describe an invalid range in array.

在流关闭后调用方法。Methods were called after the stream was closed.

示例

下面的示例读取的内容FileStream并将其写入到另一个FileStreamThe following example reads the contents from a FileStream and writes it into another 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);
    }
}
}
Imports System
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

注解

此方法重写 ReadThis method overrides Read.

offset参数指定了中的字节偏移量array(缓冲区索引) 在此处开始读取,和count参数给出的最大要从此流读取的字节数。The offset parameter gives the offset of the byte in array (the buffer index) at which to begin reading, and the count parameter gives the maximum number of bytes to be read from this stream. 如果已到达流的结尾,返回的值是字节读取,则为零的实际数目。The returned value is the actual number of bytes read, or zero if the end of the stream is reached. 如果读取的操作成功,流的当前位置前进读取的字节数。If the read operation is successful, the current position of the stream is advanced by the number of bytes read. 如果发生异常,则流的当前位置不变。If an exception occurs, the current position of the stream is unchanged.

Read方法将返回仅在到达流的末尾后的零。The Read method returns zero only after reaching the end of the stream. 否则为Read始终至少一个字节从流中读取返回之前。Otherwise, Read always reads at least one byte from the stream before returning. 没有数据时调用流中是否可用Read,直到可以返回至少一个字节的数据,将阻止该方法。If no data is available from the stream upon a call to Read, the method will block until at least one byte of data can be returned. 实现可以自由地返回少于所请求的字节,即使尚未达到流结尾。An implementation is free to return fewer bytes than requested even if the end of the stream has not been reached.

使用BinaryReader读取基元数据类型。Use BinaryReader for reading primitive data types.

不会中断正在执行的读取的操作的线程。Do not interrupt a thread that is performing a read operation. 虽然应用程序看上去要取消阻止该线程后成功运行,应用程序的性能和可靠性,可以减少中断。Although the application may appear to run successfully after the thread is unblocked, the interruption can decrease your application's performance and reliability.

常见的文件和目录操作的列表,请参阅通用 I/O 任务For a list of common file and directory operations, see Common I/O Tasks.

另请参阅

Read(Span<Byte>) Read(Span<Byte>) Read(Span<Byte>) Read(Span<Byte>)

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>

返回

适用于