明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 5525|回复: 3

解决VB读取带汉字文本文件乱码问题

[复制链接]
发表于 2006-6-12 22:46:00 | 显示全部楼层 |阅读模式

不知大家有没有这种情况,用VB读取带汉字的文件。读出的是乱码。经过我几天用GOOGLE上网搜。终于找到了解决方法。请看原代码:

Public Function readfile(ByVal filename As String) As String()
        Dim byt() As Byte
        Dim int As Integer = 0
        Dim str() As String
        Dim fs As FileStream
        Dim br As BinaryReader
        fs = New FileStream(filename, FileMode.Open)
        br = New BinaryReader(fs)
        While fs.Position < fs.Length
            ReDim Preserve byt(int)
            byt(int) = br.ReadByte
            int = int + 1
        End While
        br.Close()
        fs.Close()
        int = 0
        Dim int1 As Integer = 0
        ReDim Preserve str(int1)
        While int < byt.Length
            If byt(int) < 128 Then
                If byt(int) <> 13 And byt(int) <> 10 Then
                    str(int1) = str(int1) & Chr(byt(int))
                End If
                If byt(int) = 13 Or byt(int) = 10 Then
                    int = int + 1
                    'If str(int1).Length <> 0 Then
                    int1 = int1 + 1
                    ReDim Preserve str(int1)
                    'End If
            End If
            int = int + 1
            Else
            str(int1) = str(int1) & Chr(256 * byt(int) + byt(int + 1))
            int = int + 2
            End If
        End While
        Return str
    End Function

 楼主| 发表于 2006-6-27 21:31:00 | 显示全部楼层
自已顶一下。此帖不能沉下去。
发表于 2012-1-5 17:19:10 | 显示全部楼层
顶起楼主,谢谢
发表于 2012-1-9 23:04:47 | 显示全部楼层
似乎不用自己解码吧。
Dim strFile As String = Nothing
Using reader As New System.IO.StreamReader(fileName, System.Text.Encoding.Default)
    strFile = reader.ReadToEnd
End Using
Dim strLines() As String = strFile.Trim().Split(CChar(vbLf))
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2025-2-22 02:23 , Processed in 0.163738 second(s), 24 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表