确定文本文件是否为Unicode编码

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了确定文本文件是否为Unicode编码相关的知识,希望对你有一定的参考价值。

The .NET Framework doesn't support automatic detection of character encoding in the default file I/O methods. This is a quick function that returns True in the specified file is Unicode.
  1. Private Function is_unicode(ByVal path As String) As Boolean
  2. Dim enc As System.Text.Encoding = Nothing
  3. Dim file As System.IO.FileStream = New System.IO.FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read)
  4. If file.CanSeek Then
  5. Dim bom As Byte() = New Byte(3) {} ' Get the byte-order mark, if there is one
  6. file.Read(bom, 0, 4)
  7. If (bom(0) = &HEF AndAlso bom(1) = &HBB AndAlso bom(2) = &HBF) OrElse (bom(0) = &HFF AndAlso bom(1) = &HFE) OrElse (bom(0) = &HFE AndAlso bom(1) = &HFF) OrElse (bom(0) = 0 AndAlso bom(1) = 0 AndAlso bom(2) = &HFE AndAlso bom(3) = &HFF) Then ' ucs-4
  8. Return True
  9. Else
  10. Return False
  11. End If
  12.  
  13. ' Now reposition the file cursor back to the start of the file
  14. file.Seek(0, System.IO.SeekOrigin.Begin)
  15. Else
  16. Return False
  17. End If
  18. End Function

以上是关于确定文本文件是否为Unicode编码的主要内容,如果未能解决你的问题,请参考以下文章

如何确定文本文件的编码表

该文件含有unicode格式的字符,当文件保存为ANSI编码的文本文件时,该字符将丢失.

Java中 中文是怎么编码显示的?

如何检查TXT文本是否ANSI编码格式的?

windows操作系统unicode文本编码格式是UTF-8还是UTF-16??现在的系统

如何测试文本片段是不是是 Quoted-printable 编码的