📜  C#BinaryWriter

📅  最后修改于: 2020-10-31 04:14:11             🧑  作者: Mango

C#BinaryWriter

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...");  
        }
    }
}

输出:

Data written successfully...