Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read a file line by line with VB.NET

Tags:

file

vb.net

utf-8

The following code is used to read a file line by line.

It's just a very early version, so all I want to do is display the string in the immediate window. It's working fine, except that characters such as Ä Ü Ö è à and so on are replaced by a black square with a question mark. According to the documentation, the file reader should be compatible with UTF-8 chars so I don't know what is wrong.

...

    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

...

The file is ANSI-encoded, not UTF-8, but the reader uses UTF-8.

like image 495
user2452250 Avatar asked Jun 04 '13 14:06

user2452250


People also ask

How to read a text file line by line in VB?

The first part is here: How to open a Text File in VB .NET Quite often, you don't want to read the whole file at once. You want to read it line by line. In which case, instead of using the ReadToEnd method, as we did in the previous section, you can use the ReadLine method: The ReadLine method, as its name suggests, reads text one line at a time.

How do I read a file one line at a time?

The OpenTextFileReader method returns a StreamReader object. You can use the ReadLine method of the StreamReader object to read a file one line at a time. You can test for the end of the file using the EndOfStream method of the StreamReader object.

What does readline () and vbnewline () do?

ReadLine () & vbNewLine The first line of the Do While loop is rather curious: The Peek method takes a peek at the incoming text characters. It's looking ahead one character at a time. If it doesn't see any more characters, it will return a value of minus 1. This will signify the end of the text file.

How does the readline method work in Python?

So the ReadLine method reads each line for you, instead of the ReadToEnd method which gets the whole of the text file. Once you have a line of text in your variable, though, it's up to you to parse it. For example, suppose the line of text coming in from the text file was this:


2 Answers

Like this... I used it to read Chinese characters...

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()
like image 96
matzone Avatar answered Sep 18 '22 17:09

matzone


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

Dim reader As New StreamReader(filetoimport.Text, Encoding.Default)

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

like image 20
user2452250 Avatar answered Sep 21 '22 17:09

user2452250