用VB.NET逐行读取文件

use*_*250 9 vb.net file utf-8

以下代码用于逐行读取文件.

它只是一个非常早期的版本,所以我想做的就是在即时窗口中显示字符串.它的工作正常,除了诸如ÄÖÖèà等字符被带有问号的黑色方块所取代.根据文档,文件阅读器应该与UTF-8字符兼容,所以我不知道有什么问题.

...

    Dim reader = File.OpenText(filetoimport.Text)

    Dim line As String = Nothing

    Dim lines As Integer = 0

    While (reader.Peek() <> -1)
        line = reader.ReadLine()
        If line.StartsWith("<item key=""") Then
            Dim Firstpart As String = Nothing

            Firstpart = line.Substring(11, line.IndexOf(""" value=") - 11)

            Debug.WriteLine(Firstpart)

            lines = lines + 1

            Label3.Text = lines
            Application.DoEvents()
        Else
            Label3.Text = lines
            Application.DoEvents()
        End If

    End While

...
Run Code Online (Sandbox Code Playgroud)

该文件是ANSI编码的,不是UTF-8,但读者使用UTF-8.

mat*_*one 12

像这样......我用它来读汉字......

Dim reader as StreamReader = My.Computer.FileSystem.OpenTextFileReader(filetoimport.Text)
Dim a as String

Do
   a = reader.ReadLine
   '
   ' Code here
   '
Loop Until a Is Nothing

reader.Close()
Run Code Online (Sandbox Code Playgroud)


use*_*250 6

Replaced the reader declaration with this one and now it works!

Dim reader As New StreamReader(filetoimport.Text, Encoding.Default)
Run Code Online (Sandbox Code Playgroud)

Encoding.Default represents the ANSI code page that is set under Windows Control Panel.