解决VB读取带汉字文本文件乱码问题
<P>不知大家有没有这种情况,用VB读取带汉字的文件。读出的是乱码。经过我几天用GOOGLE上网搜。终于找到了解决方法。请看原代码:</P><P>Public Function readfile(ByVal filename As String) As String()<BR> Dim byt() As Byte<BR> Dim int As Integer = 0<BR> Dim str() As String<BR> Dim fs As FileStream<BR> Dim br As BinaryReader<BR> fs = New FileStream(filename, FileMode.Open)<BR> br = New BinaryReader(fs)<BR> While fs.Position < fs.Length<BR> ReDim Preserve byt(int)<BR> byt(int) = br.ReadByte<BR> int = int + 1<BR> End While<BR> br.Close()<BR> fs.Close()<BR> int = 0<BR> Dim int1 As Integer = 0<BR> ReDim Preserve str(int1)<BR> While int < byt.Length<BR> If byt(int) < 128 Then<BR> If byt(int) <> 13 And byt(int) <> 10 Then<BR> str(int1) = str(int1) & Chr(byt(int))<BR> End If<BR> If byt(int) = 13 Or byt(int) = 10 Then<BR> int = int + 1<BR> 'If str(int1).Length <> 0 Then<BR> int1 = int1 + 1<BR> ReDim Preserve str(int1)<BR> 'End If<BR> End If<BR> int = int + 1<BR> Else<BR> str(int1) = str(int1) & Chr(256 * byt(int) + byt(int + 1))<BR> int = int + 2<BR> End If<BR> End While<BR> Return str<BR> End Function</P> 自已顶一下。此帖不能沉下去。 顶起楼主,谢谢 似乎不用自己解码吧。
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))
页:
[1]