VB 怎么获取或判断txt文件的编码格式
展开全部
哈哈!一周前我刚刚遇到类似问题 慷慨把代码给你吧!!顺便贴上所有格式转UTF8的代码
(我本来是给XML读取用的 MSXML太挑剔了 只读UTF-8格式- - 不过转UTF-8的还是有点问题 记事本打开看上去读取没有问题 但是用MSXML读取依然有点问题。)
原理:根据文件前两个字符判断
注意 放在一个模块里面。
使用:GetEncoding(文件路径),返回一个Encoding枚举体(仿.net)
例如:
Dim ReturnEncoding As Encoding
ReturnEncoding = GetEncoding("C:\1.txt")
If ReturnEncoding = UTF8 Then
'UTF处理
ElseIf ReturnEncoding = Unicode Then
'Unicode
Elseif ReturnEncoding = UnicodeBigEndian Then
'Unicode-BE处理
Else
'ANSI处理
End If
========模块Encoding.bas==========
Option Explicit
Public Enum Encoding
ANSI
Unicode
UnicodeBigEndian
UTF8
End Enum
Public Function GetEncoding(FileName As String) As Encoding
On Error GoTo Err
Dim fBytes(1) As Byte, freeNum As Integer
freeNum = FreeFile
Open FileName For Binary Access Read As #freeNum
Get #freeNum, , fBytes(0)
Get #freeNum, , fBytes(1)
Close #freeNum
If fBytes(0) = &HFF And fBytes(1) = &HFE Then GetEncoding = Unicode
If fBytes(0) = &HFE And fBytes(1) = &HFF Then GetEncoding = UnicodeBigEndian
If fBytes(0) = &HEF And fBytes(1) = &HBB Then GetEncoding = UTF8
Err:
End Function
Public Sub FileToUTF8(FileName As String)
Dim fBytes() As Byte, uniString As String, freeNum As Integer
Dim ADO_Stream As Object
freeNum = FreeFile
ReDim fBytes(FileLen(FileName))
Open FileName For Binary Access Read As #freeNum
Get #freeNum, , fBytes
Close #freeNum
uniString = StrConv(fBytes, vbUnicode)
Set ADO_Stream = CreateObject("ADODB.Stream")
With ADO_Stream
.Type = 2
.Mode = 3
.Charset = "utf-8"
.open
.WriteText uniString
.SaveToFile FileName, 2
.Close
End With
Set ADO_Stream = Nothing
End Sub
(我本来是给XML读取用的 MSXML太挑剔了 只读UTF-8格式- - 不过转UTF-8的还是有点问题 记事本打开看上去读取没有问题 但是用MSXML读取依然有点问题。)
原理:根据文件前两个字符判断
注意 放在一个模块里面。
使用:GetEncoding(文件路径),返回一个Encoding枚举体(仿.net)
例如:
Dim ReturnEncoding As Encoding
ReturnEncoding = GetEncoding("C:\1.txt")
If ReturnEncoding = UTF8 Then
'UTF处理
ElseIf ReturnEncoding = Unicode Then
'Unicode
Elseif ReturnEncoding = UnicodeBigEndian Then
'Unicode-BE处理
Else
'ANSI处理
End If
========模块Encoding.bas==========
Option Explicit
Public Enum Encoding
ANSI
Unicode
UnicodeBigEndian
UTF8
End Enum
Public Function GetEncoding(FileName As String) As Encoding
On Error GoTo Err
Dim fBytes(1) As Byte, freeNum As Integer
freeNum = FreeFile
Open FileName For Binary Access Read As #freeNum
Get #freeNum, , fBytes(0)
Get #freeNum, , fBytes(1)
Close #freeNum
If fBytes(0) = &HFF And fBytes(1) = &HFE Then GetEncoding = Unicode
If fBytes(0) = &HFE And fBytes(1) = &HFF Then GetEncoding = UnicodeBigEndian
If fBytes(0) = &HEF And fBytes(1) = &HBB Then GetEncoding = UTF8
Err:
End Function
Public Sub FileToUTF8(FileName As String)
Dim fBytes() As Byte, uniString As String, freeNum As Integer
Dim ADO_Stream As Object
freeNum = FreeFile
ReDim fBytes(FileLen(FileName))
Open FileName For Binary Access Read As #freeNum
Get #freeNum, , fBytes
Close #freeNum
uniString = StrConv(fBytes, vbUnicode)
Set ADO_Stream = CreateObject("ADODB.Stream")
With ADO_Stream
.Type = 2
.Mode = 3
.Charset = "utf-8"
.open
.WriteText uniString
.SaveToFile FileName, 2
.Close
End With
Set ADO_Stream = Nothing
End Sub
展开全部
如果有bom头,按照bom来存取就可以了,如果没有,那么一般按ansi来处理。ms的记事本会进行很多尝试,判断有没有不在ansi范围的字符。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
一般txt文件都是有文件头的,无文件头的都是按ANSI编码来处理的,可有的UTF-8编码的txt文件也是没文件头的,如果不判断就直接读入就会出现显示为乱码。
没有文件头的txt文件,怎么判断是ANSI编码还是UTF-8编码格式呢?
没有文件头的txt文件,怎么判断是ANSI编码还是UTF-8编码格式呢?
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询