FileStream.ReadByte 方法
从文件中读取一个字节,并将读取位置提升一个字节。
**命名空间:**System.IO
**程序集:**mscorlib(在 mscorlib.dll 中)
语法
声明
Public Overrides Function ReadByte As Integer
用法
Dim instance As FileStream
Dim returnValue As Integer
returnValue = instance.ReadByte
public override int ReadByte ()
public:
virtual int ReadByte () override
public int ReadByte ()
public override function ReadByte () : int
返回值
转换为 int 的字节,或者如果从流的末尾读取则为 -1。
异常
异常类型 | 条件 |
---|---|
当前流不支持读取。 |
|
当前流已关闭。 |
备注
此方法重写 ReadByte。
给实现者的说明 Stream 的默认实现创建一个新的单字节数组,然后调用 Read。虽然这样做形式上是正确的,但效率不高。所有具有内部缓冲区的流都应重写此方法,并提供一个可以直接读取缓冲区的更为有效的版本,从而避免每次调用都要进行额外的数组分配。 下表列出了其他典型或相关的 I/O 任务的示例。
若要执行此操作... |
请参见本主题中的示例... |
---|---|
创建文本文件。 |
|
写入文本文件。 |
|
读取文本文件。 |
|
向文件中追加文本。 |
|
重命名或移动文件。 |
|
复制文件。 |
|
获取文件大小。 |
|
获取文件属性。 |
|
设置文件属性。 |
|
确定文件是否存在。 |
|
读取二进制文件。 |
|
写入二进制文件。 |
|
创建目录。 |
Directory.CreateDirectory |
示例
下面的代码示例说明如何逐字节地向文件中写入数据,然后验证数据是否被正确写入。
Imports Microsoft.VisualBasic
Imports System
Imports System.IO
Imports System.Text
Class FStream
Shared Sub Main()
Const fileName As String = "Test#@@#.dat"
' Create random data to write to the file.
Dim dataArray(100000) As Byte
Dim randomGenerator As New Random()
randomGenerator.NextBytes(dataArray)
Dim fileStream As FileStream = _
new FileStream(fileName, FileMode.Create)
Try
' Write the data to the file, byte by byte.
For i As Integer = 0 To dataArray.Length - 1
fileStream.WriteByte(dataArray(i))
Next i
' Set the stream position to the beginning of the stream.
fileStream.Seek(0, SeekOrigin.Begin)
' Read and verify the data.
For i As Integer = 0 To _
CType(fileStream.Length, Integer) - 1
If dataArray(i) <> fileStream.ReadByte() Then
Console.WriteLine("Error writing data.")
Return
End If
Next i
Console.WriteLine("The data was written to {0} " & _
"and verified.", fileStream.Name)
Finally
fileStream.Close()
End Try
End Sub
End Class
using System;
using System.IO;
class FStream
{
static void Main()
{
const string fileName = "Test#@@#.dat";
// Create random data to write to the file.
byte[] dataArray = new byte[100000];
new Random().NextBytes(dataArray);
using(FileStream
fileStream = new FileStream(fileName, FileMode.Create))
{
// Write the data to the file, byte by byte.
for(int i = 0; i < dataArray.Length; i++)
{
fileStream.WriteByte(dataArray[i]);
}
// Set the stream position to the beginning of the file.
fileStream.Seek(0, SeekOrigin.Begin);
// Read and verify the data.
for(int i = 0; i < fileStream.Length; i++)
{
if(dataArray[i] != fileStream.ReadByte())
{
Console.WriteLine("Error writing data.");
return;
}
}
Console.WriteLine("The data was written to {0} " +
"and verified.", fileStream.Name);
}
}
}
using namespace System;
using namespace System::IO;
int main()
{
String^ fileName = "Test@##@.dat";
// Create random data to write to the file.
array<Byte>^dataArray = gcnew array<Byte>(100000);
(gcnew Random)->NextBytes( dataArray );
FileStream^ fileStream = gcnew FileStream( fileName,FileMode::Create );
try
{
// Write the data to the file, byte by byte.
for ( int i = 0; i < dataArray->Length; i++ )
{
fileStream->WriteByte( dataArray[ i ] );
}
// Set the stream position to the beginning of the file.
fileStream->Seek( 0, SeekOrigin::Begin );
// Read and verify the data.
for ( int i = 0; i < fileStream->Length; i++ )
{
if ( dataArray[ i ] != fileStream->ReadByte() )
{
Console::WriteLine( "Error writing data." );
return -1;
}
}
Console::WriteLine( "The data was written to {0} "
"and verified.", fileStream->Name );
}
finally
{
fileStream->Close();
}
}
import System.*;
import System.IO.*;
class FStream
{
public static void main(String[] args)
{
final String fileName = "Test#@@#.dat";
// Create random data to write to the file.
ubyte dataArray[] = new ubyte[100000];
new Random().NextBytes(dataArray);
FileStream fileStream = new FileStream(fileName, FileMode.Create);
try {
// Write the data to the file, byte by byte.
for(int i=0;i < dataArray.length;i++) {
fileStream.WriteByte(dataArray[i]);
}
// Set the stream position to the beginning of the file.
fileStream.Seek(0, SeekOrigin.Begin);
// Read and verify the data.
for(int i=0;i < fileStream.get_Length();i++) {
if ( dataArray[i] != fileStream.ReadByte() ) {
Console.WriteLine("Error writing data.");
return;
}
}
Console.WriteLine("The data was written to {0} "
+ "and verified.", fileStream.get_Name());
}
finally {
fileStream.Dispose();
}
} //main
} //FStream
平台
Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition
.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求。
版本信息
.NET Framework
受以下版本支持:2.0、1.1、1.0
.NET Compact Framework
受以下版本支持:2.0、1.0
请参见
参考
FileStream 类
FileStream 成员
System.IO 命名空间