建立寫入器
下列程式碼範例建立寫入器,它是一個類別,可以接受某種型別的資料並將之轉換為可傳遞至資料流的位元組陣列。
Option Explicit On
Option Strict On
Imports System
Imports System.IO
Public Class MyWriter
Private s As Stream
Public Sub New(ByVal stream As Stream)
s = stream
End Sub
Public Sub WriteDouble(ByVal myData As Double)
Dim b As Byte() = BitConverter.GetBytes(myData)
' GetBytes is a binary representation of a double data type.
s.Write(b, 0, b.Length)
End Sub
Public Sub Close()
s.Close()
End Sub
End Class
using System;
using System.IO;
public class MyWriter
{
private Stream s;
public MyWriter(Stream stream)
{
s = stream;
}
public void WriteDouble(double myData)
{
byte[] b = BitConverter.GetBytes(myData);
// GetBytes is a binary representation of a double data type.
s.Write(b, 0, b.Length);
}
public void Close()
{
s.Close();
}
}
這個範例中,您建立一類別,它擁有具有資料流引數的建構函式。從這裡,您可以公開任何必要的 Write 方法。您必須轉換您所寫入 byte[] 的一切。在您取得 byte[] 之後,Write 方法會將它寫入資料流 s。