Writing a Unicode String into file

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello,
I am trying to read the contents of 01.bin (unicode) into a String, to
modify it and finally to write it back into an other file named 02.bin.

If the file really contains "a b c", then everything is replaced properly.
But the finally output looks like "ÿþb b c".
There is that prefix ÿþ. I assume that it is always printed when you work
with unicode files but I do not know how to get rid of it.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

Dim enc As System.Text.Encoding = System.Text.Encoding.Unicode

Dim a As String =
Microsoft.VisualBasic.FileIO.FileSystem.ReadAllText("01.bin", enc)

Dim b As String = Replace(a, "abc", "bbc", 1, -1)

Microsoft.VisualBasic.FileIO.FileSystem.WriteAllText("02.bin", b,
False, enc)

End Sub
 
kenny said:
I am trying to read the contents of 01.bin (unicode) into a String, to
modify it and finally to write it back into an other file named 02.bin.

If the file really contains "a b c", then everything is replaced properly.
But the finally output looks like "ÿþb b c".
There is that prefix ÿþ. I assume that it is always printed when you work
with unicode files but I do not know how to get rid of it.

It's the UTF-BOM. 'Encoding.Unicode' is actually UTF-16. You can avoid
creating the BOM as follows:

\\\
Dim enc As Encoding = New UnicodeEncoding(True, False)
Dim sw As New StreamWriter(..., enc)
///
 
Do you (or others) know, by chance, if there is also a way to avoid
insertion of the Byte Order Mark (BOM), using the binary formatter? I
have alway been curious about that....

-tom

Herfried K. Wagner [MVP] ha scritto:
 
Back
Top