Share via


StreamReader.Read メソッド

入力ストリームから次の文字または次の文字セットを読み込みます。

オーバーロードの一覧

入力ストリームから次の文字を読み込み、1 文字分だけ文字位置を進めます。

.NET Compact Framework でもサポート。

[Visual Basic] Overloads Overrides Public Function Read() As Integer

[C#] public override int Read();

[C++] public: int Read();

[JScript] public override function Read() : int;

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

.NET Compact Framework でもサポート。

[Visual Basic] Overloads Overrides Public Function Read(Char(), Integer, Integer) As Integer

[C#] public override int Read(char[], int, int);

[C++] public: int Read(__wchar_t __gc[], int, int);

[JScript] public override function Read(Char[], int, int) : int;

使用例

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

[Visual Basic, C#, C++] メモ   ここでは、Read のオーバーロード形式のうちの 1 つだけについて、使用例を示します。その他の例については、各オーバーロード形式のトピックを参照してください。

 
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++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

参照

StreamReader クラス | StreamReader メンバ | System.IO 名前空間