基になるストリームから文字を読み取り、ストリームの現在位置を進めます。
オーバーロードの一覧
基になるストリームから文字を読み取り、使用した Encoding とストリームから読み取った特定の文字に従ってストリームの現在位置を進めます。
.NET Compact Framework でもサポート。
[Visual Basic] Overloads Public Overridable Function Read() As Integer
[JScript] public function Read() : int;
index をバイト配列内の開始点として、ストリームから count で指定したバイト数分のバイトを読み取ります。
.NET Compact Framework でもサポート。
[Visual Basic] Overloads Public Overridable Function Read(Byte(), Integer, Integer) As Integer
[C++] public: virtual int Read(unsigned char __gc[], int, int);
index を文字配列内の開始点として、ストリームから count で指定した文字数分だけ文字を読み取ります。
.NET Compact Framework でもサポート。
[Visual Basic] Overloads Public Overridable Function Read(Char(), Integer, Integer) As Integer
使用例
[Visual Basic, C#, C++] メモリをバッキング ストアとして使用してデータを読み書きする方法の例を次に示します。
[Visual Basic, C#, C++] メモ ここでは、Read のオーバーロード形式のうちの 1 つだけについて、使用例を示します。その他の例については、各オーバーロード形式のトピックを参照してください。
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) = Convert.ToChar(binReader.Read())
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] = Convert.ToChar(binReader.Read());
}
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] = Convert::ToChar(binReader->Read());
}
Console::WriteLine(memoryData);
}
[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン をクリックします。