次の方法で共有


BinaryReader.ReadByte メソッド

現在のストリームから次のバイトを読み取り、ストリームの現在位置を 1 バイトだけ進めます。

Public Overridable Function ReadByte() As Byte
[C#]
public virtual byte ReadByte();
[C++]
public: virtual unsigned char ReadByte();
[JScript]
public function ReadByte() : Byte;

戻り値

現在のストリームから読み取った次のバイト。

例外

例外の種類 条件
EndOfStreamException ストリームの末尾に到達しました。
ObjectDisposedException ストリームが閉じられました。
IOException I/O エラーが発生しました。

解説

その他の一般的な I/O タスクまたは関連する I/O タスクの例を次の表に示します。

実行するタスク 参考例があるトピック
テキスト ファイルを作成する。 ファイルへのテキストの書き込み
テキスト ファイルに書き込む。 ファイルへのテキストの書き込み
テキスト ファイルから読み取る。 ファイルからのテキストの読み取り
テキストをファイルに追加する。 ログ ファイルのオープンと追加

File.AppendText

FileInfo.AppendText

ファイルのサイズを取得する。 FileInfo.Length
ファイルの属性を取得する。 File.GetAttributes
ファイルの属性を設定する。 File.SetAttributes
ファイルが存在するかどうかを判別する。 File.Exists
バイナリ ファイルから読み取る。 新しく作成したデータ ファイルの読み取りと書き込み
バイナリ ファイルに書き込む。 新しく作成したデータ ファイルの読み取りと書き込み

使用例

[Visual Basic, C#, C++] メモリをバッキング ストアとして使用してバイナリ データを書き込み、さらにデータが正常に書き込まれたことを確認する例を次に示します。

 
Imports System
Imports System.IO

Public Class BinaryRW

    Shared Sub Main()
    
        Dim i As Integer = 0

        ' Create random data to write to the stream.
        Dim writeArray(1000) As Byte
        Dim randomGenerator As New Random()
        randomGenerator.NextBytes(writeArray)

        Dim binWriter As New BinaryWriter(New MemoryStream())
        Dim binReader As New BinaryReader(binWriter.BaseStream)

        Try
        
            ' Write the data to the stream.
            Console.WriteLine("Writing the data.")
            For i = 0 To writeArray.Length - 1
                binWriter.Write(writeArray(i))
            Next i

            ' Set the stream position to the beginning of the stream.
            binReader.BaseStream.Position = 0

            ' Read and verify the data from the stream.
            For i = 0 To writeArray.Length - 1
                If binReader.ReadByte() <> writeArray(i) Then
                    Console.WriteLine("Error writing the data.")
                    Return
                End If
            Next i
            Console.WriteLine("The data was written and verified.")

        ' Catch the EndOfStreamException and write an error message.
        Catch ex As EndOfStreamException
            Console.WriteLine("Error writing the data: {0}", _
                ex.GetType().Name)
        End Try
    
    End Sub
End Class

[C#] 
using System;
using System.IO;

class BinaryRW
{
    static void Main()
    {
        int i = 0;

        // Create random data to write to the stream.
        byte[] writeArray = new byte[1000];
        new Random().NextBytes(writeArray);

        BinaryWriter binWriter = new BinaryWriter(new MemoryStream());
        BinaryReader binReader = 
            new BinaryReader(binWriter.BaseStream);

        try
        {
            // Write the data to the stream.
            Console.WriteLine("Writing the data.");
            for(i = 0; i < writeArray.Length; i++)
            {
                binWriter.Write(writeArray[i]);
            }

            // Set the stream position to the beginning of the stream.
            binReader.BaseStream.Position = 0;

            // Read and verify the data from the stream.
            for(i = 0; i < writeArray.Length; i++)
            {
                if(binReader.ReadByte() != writeArray[i])
                {
                    Console.WriteLine("Error writing the data.");
                    return;
                }
            }
            Console.WriteLine("The data was written and verified.");
        }

        // Catch the EndOfStreamException and write an error message.
        catch(EndOfStreamException e)
        {
            Console.WriteLine("Error writing the data.\n{0}",
                e.GetType().Name);
        }
    }
}

[C++] 
#using <mscorlib.dll>
using namespace System;
using namespace System::IO;

void main()
{
    int i = 0;

    // Create random data to write to the stream.
    Byte writeArray __gc[] = new Byte __gc[1000];
    (new Random())->NextBytes(writeArray);

    BinaryWriter* binWriter = new BinaryWriter(new MemoryStream());
    BinaryReader* binReader = 
        new BinaryReader(binWriter->BaseStream);

    try
    {
        // Write the data to the stream.
        Console::WriteLine(S"Writing the data.");
        for(i = 0; i < writeArray->Length; i++)
        {
            binWriter->Write(writeArray[i]);
        }

        // Set the stream position to the beginning of the stream.
        binReader->BaseStream->Position = 0;

        // Read and verify the data from the stream.
        for(i = 0; i < writeArray->Length; i++)
        {
            if(binReader->ReadByte() != writeArray[i])
            {
                Console::WriteLine(S"Error writing the data.");
                return;
            }
        }
        Console::WriteLine(S"The data was written and verified.");
    }

    // Catch the EndOfStreamException and write an error message.
    catch(EndOfStreamException* e)
    {
        Console::WriteLine(S"Error writing the data.\n{0}",
            e->GetType()->Name);
    }
}

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET

参照

BinaryReader クラス | BinaryReader メンバ | System.IO 名前空間 | 入出力操作 | ファイルからのテキストの読み取り | ファイルへのテキストの書き込み