FileStream.WriteByte(Byte) FileStream.WriteByte(Byte) FileStream.WriteByte(Byte) FileStream.WriteByte(Byte) Method

定义

一个字节写入文件流中的当前位置。Writes a byte to the current position in the file stream.

public:
 override void WriteByte(System::Byte value);
public override void WriteByte (byte value);
override this.WriteByte : byte -> unit
Public Overrides Sub WriteByte (value As Byte)

参数

value
Byte Byte Byte Byte

要写入流的字节。A byte to write to the stream.

异常

流不支持写入。The stream does not support writing.

示例

下面的代码示例演示如何将数据写入到文件,按字节,然后验证数据是否被正确写入。The following code example shows how to write data to a file, byte by byte, and then verify that the data was written correctly.

using namespace System;
using namespace System::IO;
int main()
{
   String^ fileName =  "Test@##@.dat";
   
   // Create random data to write to the file.
   array<Byte>^dataArray = gcnew array<Byte>(100000);
   (gcnew Random)->NextBytes( dataArray );
   FileStream^ fileStream = gcnew FileStream( fileName,FileMode::Create );
   try
   {
      
      // Write the data to the file, byte by byte.
      for ( int i = 0; i < dataArray->Length; i++ )
      {
         fileStream->WriteByte( dataArray[ i ] );

      }
      
      // Set the stream position to the beginning of the file.
      fileStream->Seek( 0, SeekOrigin::Begin );
      
      // Read and verify the data.
      for ( int i = 0; i < fileStream->Length; i++ )
      {
         if ( dataArray[ i ] != fileStream->ReadByte() )
         {
            Console::WriteLine( "Error writing data." );
            return  -1;
         }

      }
      Console::WriteLine( "The data was written to {0} "
      "and verified.", fileStream->Name );
   }
   finally
   {
      fileStream->Close();
   }

}

using System;
using System.IO;

class FStream
{
    static void Main()
    {
        const string fileName = "Test#@@#.dat";

        // Create random data to write to the file.
        byte[] dataArray = new byte[100000];
        new Random().NextBytes(dataArray);

        using(FileStream  
            fileStream = new FileStream(fileName, FileMode.Create))
        {
            // Write the data to the file, byte by byte.
            for(int i = 0; i < dataArray.Length; i++)
            {
                fileStream.WriteByte(dataArray[i]);
            }

            // Set the stream position to the beginning of the file.
            fileStream.Seek(0, SeekOrigin.Begin);

            // Read and verify the data.
            for(int i = 0; i < fileStream.Length; i++)
            {
                if(dataArray[i] != fileStream.ReadByte())
                {
                    Console.WriteLine("Error writing data.");
                    return;
                }
            }
            Console.WriteLine("The data was written to {0} " +
                "and verified.", fileStream.Name);
        }
    }
}
Imports Microsoft.VisualBasic
Imports System
Imports System.IO
Imports System.Text

Class FStream

    Shared Sub Main()

        Const fileName As String = "Test#@@#.dat"

        ' Create random data to write to the file.
        Dim dataArray(100000) As Byte
        Dim randomGenerator As New Random()
        randomGenerator.NextBytes(dataArray)

        Dim fileStream As FileStream = _
            new FileStream(fileName, FileMode.Create)
        Try

            ' Write the data to the file, byte by byte.
            For i As Integer = 0 To dataArray.Length - 1
                fileStream.WriteByte(dataArray(i))
            Next i

            ' Set the stream position to the beginning of the stream.
            fileStream.Seek(0, SeekOrigin.Begin)

            ' Read and verify the data.
            For i As Integer = 0 To _
                CType(fileStream.Length, Integer) - 1

                If dataArray(i) <> fileStream.ReadByte() Then
                    Console.WriteLine("Error writing data.")
                    Return
                End If
            Next i
            Console.WriteLine("The data was written to {0} " & _
                "and verified.", fileStream.Name)
        Finally
            fileStream.Close()
        End Try
    
    End Sub
End Class

注解

此方法重写 WriteByteThis method overrides WriteByte.

使用WriteByte写入到一个字节FileStream高效。Use WriteByte to write a byte to a FileStream efficiently. 如果流已关闭或不可写,将引发异常。If the stream is closed or not writable, an exception will be thrown.

备注

使用CanWrite属性来确定当前实例是否支持写入。Use the CanWrite property to determine whether the current instance supports writing. 有关附加信息,请参见 CanWriteFor additional information, see CanWrite.

继承者说明

上的默认实现Stream创建一个新的单字节数组,然后调用Write(Byte[], Int32, Int32)The default implementation on Stream creates a new single-byte array and then calls Write(Byte[], Int32, Int32). 虽然这是正式正确,它是低效的。While this is formally correct, it is inefficient. 所有具有内部缓冲区的流应重写此方法并提供一个高效得多的读取缓冲区直接,避免在每次调用额外的数组分配的版本。Any stream with an internal buffer should override this method and provide a much more efficient version that reads the buffer directly, avoiding the extra array allocation on every call.

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

适用于

另请参阅