http://www.motobit.com/tips/detpg_read- ... ary-files/
http://www.xefteri.com/articles/show.cfm?id=7
以下為表列讀寫 function, 和上面網址多加了 close 及 set nothing 來結束.
代碼: 選擇全部
Function SaveTextData(FileName, Text, CharSet)
Const adTypeText = 2
Const adSaveCreateOverWrite = 2
'Create Stream object
Dim BinaryStream
Set BinaryStream = CreateObject("ADODB.Stream")
'Specify stream type - we want To save text/string data.
BinaryStream.Type = adTypeText
'Specify charset For the source text (unicode) data.
If Len(CharSet) > 0 Then
BinaryStream.CharSet = CharSet
End If
'Open the stream And write binary data To the object
BinaryStream.Open
BinaryStream.WriteText Text
'Save binary data To disk
BinaryStream.SaveToFile FileName, adSaveCreateOverWrite
BinaryStream.Close
Set BinaryStream = Nothing
End Function
Function ReadTextFile(FileName, CharSet)
Const adTypeText = 2
'Create Stream object
Dim BinaryStream
Set BinaryStream = CreateObject("ADODB.Stream")
'Specify stream type - we want To get binary data.
BinaryStream.Type = adTypeText
'Specify charset For the source text (unicode) data.
If Len(CharSet) > 0 Then
BinaryStream.CharSet = CharSet
End If
'Open the stream
BinaryStream.Open
'Load the file data from disk To stream object
BinaryStream.LoadFromFile FileName
'Open the stream And get binary data from the object
ReadTextFile = BinaryStream.ReadText
BinaryStream.Close
Set BinaryStream = Nothing
End Function
要特別注意的是這個寫入的 utf-8 檔案, 會是有檔頭的 utf-8 (也就是 utf-8 with signature)的格式哦! 不過有趣的地方是讀檔的部分是無論有無檔頭的 utf-8 都支援, 也就是 utf-8 without signature 的格式也沒問題哦!