C# BinaryWriter類

C# BinaryWriter類用於將二進制信息寫入流中。它在System.IO命名空間中找到。它還支持以特定編碼編寫字符串。

C# BinaryWriter示例

我們來看看將數據寫入數據文件的BinaryWriter類的簡單例子。

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(1024);  
                writer.Write("this is string data");  
                writer.Write(true);  
            }  
            Console.WriteLine("Data written successfully...");    
        }  
    }  
}

執行上面示例代碼,得到以下結果 -

Data written successfully...