C#BinaryWriter类用于将二进制信息写入流。在System.IO命名空间中找到它。它还支持以特定编码写入字符串。
C#BinaryWriter示例
让我们看一下BinaryWriter类的简单示例,该类将数据写入dat文件。
using System;
using System.IO;
namespace BinaryWriterExample
{
class Program
{
static void Main(string[] args)
{
string fileName = "e:\\binaryfile.dat";
using (BinaryWriter writer = new BinaryWriter(File.Open(fileName, FileMode.Create)))
{
writer.Write(2.5);
writer.Write("this is string data");
writer.Write(true);
}
Console.WriteLine("Data written successfully...");
}
}
}
【C# BinaryWriter用法】输出:
Data written successfully...
推荐阅读
- C# StringReader用法
- C# StringWriter用法
- C# BinaryReader用法
- C# TextReader用法
- C# TextWriter用法
- C#用户定义的异常
- C#文件流
- C# finally块
- C# try-catch用法