次の方法で共有


BinaryReader.ReadChar メソッド

現在のストリームの次の文字を読み取り、使用した Encoding とストリームから読み取った特定の文字に従ってストリームの現在位置を進めます。

Public Overridable Function ReadChar() As Char
[C#]
public virtual char ReadChar();
[C++]
public: virtual __wchar_t ReadChar();
[JScript]
public function ReadChar() : Char;

戻り値

現在のストリームから読み取った文字。

例外

例外の種類 条件
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
        Dim invalidPathChars() As Char = Path.InvalidPathChars
        Dim memStream As new MemoryStream()
        Dim binWriter As New BinaryWriter(memStream)

        ' Write to memory.
        binWriter.Write("Invalid file path characters are: ")
        For i = 0 To invalidPathChars.Length - 1
            binWriter.Write(invalidPathChars(i))
        Next i

        ' Create the reader using the same MemoryStream 
        ' as used with the writer.
        Dim binReader As New BinaryReader(memStream)

        ' Set Position to the beginning of the stream.
        memStream.Position = 0

        ' Read the data from memory and write it to the console.
        Console.Write(binReader.ReadString())
        Dim memoryData( _
            CInt(memStream.Length - memStream.Position) - 1) As Char
        For i = 0 To memoryData.Length - 1
            memoryData(i) = binReader.ReadChar()
        Next i
        Console.WriteLine(memoryData)
    
    End Sub
End Class

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

class BinaryRW
{
    static void Main()
    {
        int i = 0;
        char[] invalidPathChars = Path.InvalidPathChars;
        MemoryStream memStream = new MemoryStream();
        BinaryWriter binWriter = new BinaryWriter(memStream);

        // Write to memory.
        binWriter.Write("Invalid file path characters are: ");
        for(i = 0; i < invalidPathChars.Length; i++)
        {
            binWriter.Write(invalidPathChars[i]);
        }

        // Create the reader using the same MemoryStream 
        // as used with the writer.
        BinaryReader binReader = new BinaryReader(memStream);

        // Set Position to the beginning of the stream.
        memStream.Position = 0;

        // Read the data from memory and write it to the console.
        Console.Write(binReader.ReadString());
        char[] memoryData = 
            new char[memStream.Length - memStream.Position];
        for(i = 0; i < memoryData.Length; i++)
        {
            memoryData[i] = binReader.ReadChar();
        }
        Console.WriteLine(memoryData);
    }
}

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

void main()
{
    int i;
    Char invalidPathChars __gc[] = Path::InvalidPathChars;
    MemoryStream* memStream = new MemoryStream();
    BinaryWriter* binWriter = new BinaryWriter(memStream);

    // Write to memory.
    binWriter->Write(S"Invalid file path characters are: ");
    for(i = 0; i < invalidPathChars->Length; i++)
    {
        binWriter->Write(invalidPathChars[i]);
    }

    // Create the reader using the same MemoryStream 
    // as used with the writer.
    BinaryReader* binReader = new BinaryReader(memStream);

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

    // Read the data from memory and write it to the console.
    Console::Write(binReader->ReadString());
    Char memoryData __gc[] = 
        new Char __gc[memStream->Length - memStream->Position];
    for(i = 0; i < memoryData->Length; i++)
    {
        memoryData[i] = binReader->ReadChar();
    }
    Console::WriteLine(memoryData);
}

[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 名前空間 | Encoding | 入出力操作 | ファイルからのテキストの読み取り | ファイルへのテキストの書き込み