index をバイト配列内の開始点として、ストリームから count で指定したバイト数分のバイトを読み取ります。
Overloads Public Overridable Function Read( _
ByVal buffer() As Byte, _ ByVal index As Integer, _ ByVal count As Integer _) As Integer
[C#]
public virtual int Read(byte[] buffer,intindex,intcount);
[C++]
public: virtual int Read(unsigned charbuffer __gc[],intindex,intcount);
[JScript]
public function Read(
buffer : Byte[],index : int,count : int) : int;
パラメータ
- buffer
データを読み取るバッファ。 - index
バッファ内の、バッファへの読み取り開始点。 - count
読み取る文字の数。
戻り値
buffer に読み取った文字数。要求したバイト数分のバイトを読み取れなかった場合、この値は要求したバイト数より小さくなります。ストリームの末尾に到達した場合は 0 になることがあります。
例外
| 例外の種類 | 条件 |
|---|---|
| ArgumentException | バッファ長から index を差し引いた値が count より小さい値です。 |
| ArgumentNullException | buffer が null 参照 (Visual Basic では Nothing) です。 |
| ArgumentOutOfRangeException | index または count が負の値です。 |
| ObjectDisposedException | ストリームが閉じられました。 |
| IOException | I/O エラーが発生しました。 |
解説
その他の一般的な I/O タスクまたは関連する I/O タスクの例を次の表に示します。
| 実行するタスク | 参考例があるトピック |
|---|---|
| テキスト ファイルを作成する。 | ファイルへのテキストの書き込み |
| テキスト ファイルに書き込む。 | ファイルへのテキストの書き込み |
| テキスト ファイルから読み取る。 | ファイルからのテキストの読み取り |
| テキストをファイルに追加する。 | ログ ファイルのオープンと追加 |
| ファイルのサイズを取得する。 | FileInfo.Length |
| ファイルの属性を取得する。 | File.GetAttributes |
| ファイルの属性を設定する。 | File.SetAttributes |
| ファイルが存在するかどうかを判別する。 | File.Exists |
| バイナリ ファイルから読み取る。 | 新しく作成したデータ ファイルの読み取りと書き込み |
| バイナリ ファイルに書き込む。 | 新しく作成したデータ ファイルの読み取りと書き込み |
使用例
[Visual Basic, C#, C++] メモリをバッキング ストアとして使用してバイナリ データを書き込み、さらにデータが正常に書き込まれたことを確認する例を次に示します。
Imports System
Imports System.IO
Public Class BinaryRW
Shared Sub Main()
Const upperBound As Integer = 1000
' Create random data to write to the stream.
Dim dataArray(upperBound) As Byte
Dim randomGenerator As New Random
randomGenerator.NextBytes(dataArray)
Dim binWriter As New BinaryWriter(New MemoryStream())
' Write the data to the stream.
Console.WriteLine("Writing the data.")
binWriter.Write(dataArray, 0, dataArray.Length)
' Create the reader using the stream from the writer.
Dim binReader As New BinaryReader(binWriter.BaseStream)
' Set Position to the beginning of the stream.
binReader.BaseStream.Position = 0
' Read and verify the data.
Dim verifyArray(upperBound) As Byte
If binReader.Read(verifyArray, 0, dataArray.Length) _
<> dataArray.Length Then
Console.WriteLine("Error writing the data.")
Return
End If
For i As Integer = 0 To upperBound
If verifyArray(i) <> dataArray(i) Then
Console.WriteLine("Error writing the data.")
Return
End If
Next i
Console.WriteLine("The data was written and verified.")
End Sub
End Class
[C#]
using System;
using System.IO;
class BinaryRW
{
static void Main()
{
const int arrayLength = 1000;
// Create random data to write to the stream.
byte[] dataArray = new byte[arrayLength];
new Random().NextBytes(dataArray);
BinaryWriter binWriter = new BinaryWriter(new MemoryStream());
// Write the data to the stream.
Console.WriteLine("Writing the data.");
binWriter.Write(dataArray, 0, arrayLength);
// Create the reader using the stream from the writer.
BinaryReader binReader =
new BinaryReader(binWriter.BaseStream);
// Set Position to the beginning of the stream.
binReader.BaseStream.Position = 0;
// Read and verify the data.
byte[] verifyArray = new byte[arrayLength];
if(binReader.Read(verifyArray, 0, arrayLength) != arrayLength)
{
Console.WriteLine("Error writing the data.");
return;
}
for(int i = 0; i < arrayLength; i++)
{
if(verifyArray[i] != dataArray[i])
{
Console.WriteLine("Error writing the data.");
return;
}
}
Console.WriteLine("The data was written and verified.");
}
}
[C++]
#using <mscorlib.dll>
using namespace System;
using namespace System::IO;
void main()
{
const int arrayLength = 1000;
// Create random data to write to the stream.
Byte dataArray __gc[] = new Byte __gc[arrayLength];
(new Random())->NextBytes(dataArray);
BinaryWriter* binWriter =
new BinaryWriter(new MemoryStream());
// Write the data to the stream.
Console::WriteLine(S"Writing the data.");
binWriter->Write(dataArray, 0, arrayLength);
// Create the reader using the stream from the writer.
BinaryReader* binReader =
new BinaryReader(binWriter->BaseStream);
// Set Position to the beginning of the stream.
binReader->BaseStream->Position = 0;
// Read and verify the data.
Byte verifyArray __gc[] = new Byte __gc[arrayLength];
if(binReader->Read(
verifyArray, 0, arrayLength) != arrayLength)
{
Console::WriteLine(S"Error writing the data.");
return;
}
for(int i = 0; i < arrayLength; i++)
{
if(verifyArray[i] != dataArray[i])
{
Console::WriteLine(S"Error writing the data.");
return;
}
}
Console::WriteLine(S"The data was written and verified.");
}
[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 名前空間 | BinaryReader.Read オーバーロードの一覧 | 入出力操作 | ファイルからのテキストの読み取り | ファイルへのテキストの書き込み