Vb.net 从文件流中读取UTF8字符
我需要一种从Vb.net 从文件流中读取UTF8字符,vb.net,filestream,utf,Vb.net,Filestream,Utf,我需要一种从FileStream读取每个字符的方法。逐字逐句。 每次读取字符时,我都需要增加FileStream.Position 我正在尝试代码段,但它返回多个字符: Dim bytes(1) As Byte Dim nBytes As Integer = oFile.Read(bytes, 0, bytes.Length) Dim nChars As Integer = decoder8.GetCharCount(bytes, 0, nBytes) Dim chars(nChars - 1
FileStream
读取每个字符的方法。逐字逐句。
每次读取字符时,我都需要增加FileStream.Position
我正在尝试代码段,但它返回多个字符:
Dim bytes(1) As Byte
Dim nBytes As Integer = oFile.Read(bytes, 0, bytes.Length)
Dim nChars As Integer = decoder8.GetCharCount(bytes, 0, nBytes)
Dim chars(nChars - 1) As Char
nChars = decoder8.GetChars(bytes, 0, nBytes, chars, 0)
Return New String(chars, 0, nChars)
您可以使用
StreamReader
及其方法,不是吗
Using rd = New StreamReader("path", Encoding.UTF8)
While rd.Peek() >= 0
Dim c As Char = Chr(rd.Read())
Console.WriteLine("Next character: " & c)
End While
End Using
StreamReader.Read
从输入流中读取下一个字符并前进
一个字符的字符位置
假设文件的
类型为FileStream
,则
Dim nBytes As Integer = oFile.ReadByte()
应该可以工作。不,我不能,因为FileStream在特定名称空间中使用了多次