不知大家有没有这种情况,用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 |