读文本文件出现乱码
Imports System.IOModule Module1
Sub Main()
Dim objStreamReader As StreamReader
Dim strLine As String
'Pass the file path and the file name to the StreamReader constructor.
objStreamReader = New StreamReader("C:\Users\zhaowenqiao\Documents\7.txt")
'Read the first line of text.
strLine = objStreamReader.ReadLine
'Continue to read until you reach the end of the file.
Do While Not strLine Is Nothing
'Write the line to the Console window.
Console.WriteLine(strLine)
'Read the next line.
strLine = objStreamReader.ReadLine
Loop
'Close the file.
objStreamReader.Close()
Console.ReadLine()
End Sub
End Module
读文本的时候都是乱码,这是怎么回事 --------------------编程问答-------------------- 编码问题 --------------------编程问答-------------------- 试试
System.IO.StreamReader sr=new System.IO.StreamReader(@"C:\Users\zhaowenqiao\Documents\7.txt",System.Text.Encoding.UTF8); --------------------编程问答-------------------- Dim rdr As New IO.StreamReader("C:\Users\zhaowenqiao\Documents\7.txt",System.Text.Encoding.Default)
or
Dim rdr As New IO.StreamReader("C:\Users\zhaowenqiao\Documents\7.txt",System.Text.Encoding.GetEncoding("Gb2312") --------------------编程问答-------------------- 问题出在:
objStreamReader = New StreamReader("C:\Users\zhaowenqiao\Documents\7.txt")
使用streamreader时,必须注意括号里面的参数:
把这段代码改为:
objStreamReader = New StreamReader("C:\Users\zhaowenqiao\Documents\7.txt",System.Text.Encoding.Default)
你自己试试吧,System.Text.Encoding.Default意思是让系统自动选择合适的解码读取数据流,你原先代码没有解码参数,所以会出现乱码。
当然,还有其他解码,看问题了。 --------------------编程问答-------------------- 你的程序还有别的问题吗
我现在在线了
--------------------编程问答-------------------- 上QQ吧
补充:.NET技术 , VB.NET