StreamReader.Read メソッド
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
入力ストリームから次の文字または次の文字セットを読み取ります。
オーバーロード
| 名前 | 説明 |
|---|---|
| Read() |
入力ストリームから次の文字を読み取り、文字位置を 1 文字進めます。 |
| Read(Span<Char>) |
現在のストリームからスパンに文字を読み取ります。 |
| Read(Char[], Int32, Int32) |
指定したインデックスから始まる、現在のストリームからバッファーに指定された最大文字数を読み取ります。 |
Read()
- ソース:
- StreamReader.cs
- ソース:
- StreamReader.cs
- ソース:
- StreamReader.cs
- ソース:
- StreamReader.cs
- ソース:
- StreamReader.cs
入力ストリームから次の文字を読み取り、文字位置を 1 文字進めます。
public:
override int Read();
public override int Read();
override this.Read : unit -> int
Public Overrides Function Read () As Integer
返品
Int32 オブジェクトとして表される入力ストリームの次の文字。使用できる文字がなくなった場合は -1。
例外
I/O エラーが発生しました。
例
次のコード例は、 Read メソッドを簡単に使用する方法を示しています。
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))
{
while (sr.Peek() >= 0)
{
Console.Write((char)sr.Read());
}
}
}
catch (Exception e)
{
Console.WriteLine("The process failed: {0}", e.ToString());
}
}
}
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
Console.Write(Convert.ToChar(sr.Read()))
Loop
sr.Close()
Catch e As Exception
Console.WriteLine("The process failed: {0}", e.ToString())
End Try
End Sub
End Class
次のコード例では、 Read() メソッドのオーバーロードを使用して 1 文字を読み取り、ASCII 整数出力を 10 進数および 16 進数として書式設定する方法を示します。
using System;
using System.IO;
class StrmRdrRead
{
public static void Main()
{
//Create a FileInfo instance representing an existing text file.
FileInfo MyFile=new FileInfo(@"c:\csc.txt");
//Instantiate a StreamReader to read from the text file.
StreamReader sr=MyFile.OpenText();
//Read a single character.
int FirstChar=sr.Read();
//Display the ASCII number of the character read in both decimal and hexadecimal format.
Console.WriteLine("The ASCII number of the first character read is {0:D} in decimal and {1:X} in hexadecimal.",
FirstChar, FirstChar);
//
sr.Close();
}
}
Imports System.IO
Class StrmRdrRead
Public Shared Sub Main()
'Create a FileInfo instance representing an existing text file.
Dim MyFile As New FileInfo("c:\csc.txt")
'Instantiate a StreamReader to read from the text file.
Dim sr As StreamReader = MyFile.OpenText()
'Read a single character.
Dim FirstChar As Integer = sr.Read()
'Display the ASCII number of the character read in both decimal and hexadecimal format.
Console.WriteLine("The ASCII number of the first character read is {0:D} in decimal and {1:X} in hexadecimal.", FirstChar, FirstChar)
sr.Close()
End Sub
End Class
注釈
このメソッドは、TextReader.Read をオーバーライドします。
このメソッドは、ストリームの末尾に達した場合に -1 を返すことができるように、整数を返します。 バッファーへのデータの読み取り後に基になるストリームの位置を操作すると、基になるストリームの位置が内部バッファーの位置と一致しない可能性があります。 内部バッファーをリセットするには、 DiscardBufferedData メソッドを呼び出します。ただし、このメソッドはパフォーマンスを低下させ、絶対に必要な場合にのみ呼び出す必要があります。
一般的な I/O タスクの一覧については、「 一般的な I/O タスク」を参照してください。
こちらもご覧ください
適用対象
Read(Span<Char>)
- ソース:
- StreamReader.cs
- ソース:
- StreamReader.cs
- ソース:
- StreamReader.cs
- ソース:
- StreamReader.cs
- ソース:
- StreamReader.cs
現在のストリームからスパンに文字を読み取ります。
public:
override int Read(Span<char> buffer);
public override int Read(Span<char> buffer);
override this.Read : Span<char> -> int
Public Overrides Function Read (buffer As Span(Of Char)) As Integer
パラメーター
返品
読み取られた文字数。ストリームの末尾でデータが読み取られなかった場合は 0。 データがストリーム内で使用可能かどうかに応じて、この数値は buffer 長以下になります。
例外
ストリームから読み取られた文字数が、 buffer の長さを超えています。
buffer は nullです。
適用対象
Read(Char[], Int32, Int32)
- ソース:
- StreamReader.cs
- ソース:
- StreamReader.cs
- ソース:
- StreamReader.cs
- ソース:
- StreamReader.cs
- ソース:
- StreamReader.cs
指定したインデックスから始まる、現在のストリームからバッファーに指定された最大文字数を読み取ります。
public:
override int Read(cli::array <char> ^ buffer, int index, int count);
public override int Read(char[] buffer, int index, int count);
override this.Read : char[] * int * int -> int
Public Overrides Function Read (buffer As Char(), index As Integer, count As Integer) As Integer
パラメーター
- buffer
- Char[]
このメソッドから制御が戻るときに、指定した文字配列を、現在のソースから読み取った文字に置き換えた index と (index + count - 1) の間の値を格納します。
- index
- Int32
書き込みを開始する buffer のインデックス。
- count
- Int32
読み取る最大文字数。
返品
読み取られた文字数。ストリームの末尾でデータが読み取られなかった場合は 0。 数値は、ストリーム内でデータが使用可能かどうかに応じて、 count パラメーター以下になります。
例外
バッファーの長さから index を引いた値は、count未満です。
buffer は nullです。
index または count が負の値です。
ストリームが閉じているなど、I/O エラーが発生します。
例
次のコード例では、ファイルの末尾に達するまで一度に 5 文字を読み取ります。
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.ReadBlock(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());
}
}
}
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.ReadBlock(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
注釈
このメソッドは、TextReader.Read をオーバーライドします。
このメソッドは、ストリームの末尾に達した場合に 0 を返すことができるように、整数を返します。
Readメソッドを使用する場合は、ストリームの内部バッファーと同じサイズのバッファーを使用する方が効率的です。このバッファーでは、内部バッファーが目的のブロック サイズに設定され、常にブロック サイズより小さい値を読み取ります。 ストリームの作成時に内部バッファーのサイズが指定されていない場合、既定のサイズは 4 KB (4096 バイト) です。 バッファーへのデータの読み取り後に基になるストリームの位置を操作すると、基になるストリームの位置が内部バッファーの位置と一致しない可能性があります。 内部バッファーをリセットするには、 DiscardBufferedData メソッドを呼び出します。ただし、このメソッドはパフォーマンスを低下させ、絶対に必要な場合にのみ呼び出す必要があります。
このメソッドは、 count パラメーターで指定された文字数が読み取られた後、またはファイルの末尾に達した後に返されます。
ReadBlock は、 Readのブロック バージョンです。
一般的な I/O タスクの一覧については、「 一般的な I/O タスク」を参照してください。