微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

转:VB/VBScript读取和保存UTF-8文件方案

转: http://www.cnblogs.com/waver/articles/1283842.html

=============================== VB Code ===============================

Public Function SaveFile(FileName As Variant,strFileBody As Variant) As Boolean

Dim ADO_Stream As Object
Set ADO_Stream = CreateObject("ADODB.Stream")

With ADO_Stream
.Type = 2
.Mode = 3
.Charset = "utf-8"
.Open
.WriteText strFileBody
.SavetoFile FileName,2
End With

SaveFile = True
Set ADO_Stream = nothing
End Function

Public Function ReadUTF8(ByVal sUTF8File As String) As String
If Len(sUTF8File) = 0 Or Dir(sUTF8File) = vbNullString Then Exit Function
Dim ados As Object
Set ados = CreateObject("adodb.stream")
With ados
.Charset = "utf-8"
.Type = 2
.Open
.LoadFromFile sUTF8File
ReadUTF8 = .ReadText
.Close
End With
Set ados = nothing
End Function

=============================== VBScript Code ===============================

Function LoadFile(Path)
Dim Stm2
Set Stm2 = CreateObject("ADODB.Stream")
Stm2.Type = 2
Stm2.Mode = 3
Stm2.Open

Stm2.LoadFromFile Path
Stm2.Charset = "UTF-8"
'Stm2.Charset = "Unicode"
'Stm2.Charset = "GB2312"

Stm2.position = 0
LoadFile = Stm2.ReadText
Stm2.Close
Set Stm2 = nothing
End Function

Function WritetoFile(file,Message) Dim Stm1 Set Stm1 = CreateObject("ADODB.Stream") Stm1.Type = 2 Stm1.Open Stm1.Charset = "UTF-8" 'Stm1.Charset = "Unicode" Stm1.Position = Stm1.Size Stm1.WriteText LoadFile(file) + vbCrLf + Message Stm1.SavetoFile file,2 Stm1.Close set Stm1 = nothing End Function

原文地址:https://www.jb51.cc/vb/262838.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐