Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27159

Reading and writing UTF8 file?

$
0
0
Hello

This is probably an easy issue for experts but Google didn't help.

I need to read a text file that contains addresses formatted thusly:
Code:

Address1
Address1
Address1

Address2
Address2
Address2
etc.

... and write them into this:
Code:

Address1, Address1, Address1
Address2, Address2, Address2
etc.

The data contains East European characters.

The following code works mostly, but if I don't specify any codeset for the output, it's saved as ASCII (DOC charset), while I get a compile error if I add "Encoding.UTF8" ("Error 1 Overload resolution failed because no accessible 'New' can be called with these arguments").

Code:

Dim objReader As New StreamReader("c:\input.utf8.txt", Encoding.Default)


'BAD : ASCII Dim objWriter As New StreamWriter("c:\output.utf8.txt")
'Compile error
Dim objWriter As New StreamWriter("c:\output.utf8.txt",Encoding.UTF8)


Dim Line As String
Dim WholeFile As String = Nothing

Do While objReader.Peek() <> -1
    Line = objReader.ReadLine
    If Line.Length = 0 Then
        WholeFile = WholeFile.Substring(0, WholeFile.Length - 2)
        WholeFile = WholeFile + vbCrLf
        RichTextBox1.Text = WholeFile
    Else
        WholeFile = WholeFile + Line + ", "
    End If
Loop

objWriter.Write(WholeFile)

objWriter.Close()
objReader.Close()

Does someone know what must be done for VB.Net to handle UTF8 as expected?

Thank you.

Viewing all articles
Browse latest Browse all 27159

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>