FileStream.WriteByte 方法
将一个字节写入文件流的当前位置。
**命名空间:**System.IO
**程序集:**mscorlib(在 mscorlib.dll 中)
语法
声明
Public Overrides Sub WriteByte ( _
value As Byte _
)
用法
Dim instance As FileStream
Dim value As Byte
instance.WriteByte(value)
public override void WriteByte (
byte value
)
public:
virtual void WriteByte (
unsigned char value
) override
public void WriteByte (
byte value
)
public override function WriteByte (
value : byte
)
参数
- value
要写入流的字节。
异常
异常类型 | 条件 |
---|---|
流已关闭。 |
|
流不支持写入。 |
备注
此方法重写 WriteByte。
使用 WriteByte 高效地向 FileStream 写入一个字节。如果流是关闭的或是不可写的,将会引发异常。
给实现者的说明 Stream 的默认实现创建一个新的单字节数组,然后调用 Write。虽然这样做形式上是正确的,但效率不高。所有具有内部缓冲区的流都应重写此方法,并提供一个可以直接读取缓冲区的更为有效的版本,从而避免每次调用都要进行额外的数组分配。 下表列出了其他典型或相关的 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 命名空间