次の方法で共有


StreamReader.Read メソッド (Char , Int32, Int32)

現在のストリームの index で示された位置から、 count で指定された最大文字数を buffer に読み込みます。

Overrides Overloads Public Function Read( _
   <InteropServices.In(), _
   Out()> ByVal buffer() As Char, _   ByVal index As Integer, _   ByVal count As Integer _) As Integer
[C#]
public override int Read(   [   In,   Out] char[] buffer,intindex,intcount);
[C++]
public: int Read(   [   In,   Out] __wchar_tbuffer __gc[],intindex,intcount);
[JScript]
public override function Read(
   buffer : Char[],index : int,count : int) : int;

パラメータ

  • buffer
    このメソッドが戻るとき、指定した文字配列の index から (index + count - 1) までの値が、現在のソースから読み取られた文字に置き換えられています。
  • index
    書き込みの開始位置を示す buffer のインデックス。
  • count
    読み取り対象の最大文字数。

戻り値

読み込まれた文字数。ストリームの末尾でデータが読み込まれなかった場合は 0。この数値は、ストリーム内に使用できるデータがあるかどうかによって異なりますが、 count 以下の数値になります。

例外

例外の種類 条件
ArgumentException バッファ長から index を差し引いた値が count より小さい値です。
ArgumentNullException buffer が null 参照 (Visual Basic では Nothing) です。
ArgumentOutOfRangeException index または count が負の値です。
IOException I/O エラーが発生しました。たとえば、ストリームがクローズされています。

解説

このメソッドは、 Read をオーバーライドします。

このメソッドは整数を返し、ストリームの末尾に到達すると -1 を返します。

Read メソッドを使用するときは、ストリームの内部バッファと同じサイズのバッファを使用すると効率的です。ストリームの生成時に内部バッファのサイズを指定しなかった場合は、既定値の 4 KB (4096 バイト) に設定されます。

count で指定した文字数を読み込んだ後、またはファイルの末尾に到達した後に、このメソッドを返します。 ReadBlock は、 StreamReader.Read のブロッキング バージョンです。

このメソッドの使用例については、以下の「使用例」を参照してください。その他の一般的な I/O タスクまたは関連する I/O タスクの例を次の表に示します。

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

File.AppendText

FileInfo.AppendText

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

使用例

[Visual Basic, C#, C++] ファイルの末尾に到達するまで、一度に 5 文字ずつ読み取る例を次に示します。

 
Imports System
Imports System.IO
Imports System.Text

Public Class Test

    Public Shared Sub Main()
        Dim path As String = "c:\temp\MyTest.txt"

        Try
            If File.Exists(path) Then
                File.Delete(path)
            End If

            Dim sw As StreamWriter = New StreamWriter(path)
            sw.WriteLine("This")
            sw.WriteLine("is some text")
            sw.WriteLine("to test")
            sw.WriteLine("Reading")
            sw.Close()

            Dim sr As StreamReader = New StreamReader(path)

            Do While sr.Peek() >= 0
                'This is an arbitrary size for this example.
                Dim c(5) As Char
                sr.Read(c, 0, c.Length)
                'The output will look odd, because
                'only five characters are read at a time.
                Console.WriteLine(c)
            Loop
            sr.Close()
        Catch e As Exception
            Console.WriteLine("The process failed: {0}", e.ToString())
        End Try
    End Sub
End Class

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

class Test 
{
    
    public static void Main() 
    {
        string path = @"c:\temp\MyTest.txt";

        try 
        {
            if (File.Exists(path)) 
            {
                File.Delete(path);
            }

            using (StreamWriter sw = new StreamWriter(path)) 
            {
                sw.WriteLine("This");
                sw.WriteLine("is some text");
                sw.WriteLine("to test");
                sw.WriteLine("Reading");
            }

            using (StreamReader sr = new StreamReader(path)) 
            {
                //This is an arbitrary size for this example.
                char[] c = null;

                while (sr.Peek() >= 0) 
                {
                    c = new char[5];
                    sr.Read(c, 0, c.Length);
                    //The output will look odd, because
                    //only five characters are read at a time.
                    Console.WriteLine(c);
                }
            }
        } 
        catch (Exception e) 
        {
            Console.WriteLine("The process failed: {0}", e.ToString());
        }
    }
}

[C++] 
#using <mscorlib.dll>

using namespace System;
using namespace System::IO;

int main() {
    String* path = S"c:\\temp\\MyTest.txt";

    try {
        if (File::Exists(path)) {
            File::Delete(path);
        }

        StreamWriter* sw = new StreamWriter(path);
        try {
            sw->WriteLine(S"This");
            sw->WriteLine(S"is some text");
            sw->WriteLine(S"to test");
            sw->WriteLine(S"Reading");
        } __finally {
            if (sw) __try_cast<IDisposable*>(sw)->Dispose();
        }

        StreamReader* sr = new StreamReader(path);
        try {
            //This is an arbitrary size for this example.
            Char c[] = 0;

            while (sr->Peek() >= 0) {
                c = new Char[5];
                sr->Read(c, 0, c->Length);
                //The output will look odd, because
                //only five characters are read at a time.
                Console::WriteLine(c);
            }
        } __finally {
            if (sr) __try_cast<IDisposable*>(sr)->Dispose();
        }
    } catch (Exception* e) {
        Console::WriteLine(S"The process failed: {0}", e);
    }
}

[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, Common Language Infrastructure (CLI) Standard

参照

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