Convert byte from a file to UTF8 : Text File Write « File Directory « VB.Net






Convert byte from a file to UTF8

 

Imports System
Imports System.IO
Imports System.Text

Public Class Test
    Public Shared Sub Main()
        Dim path As String = "c:\MyTest.txt"
        Dim fs As FileStream = File.Create(path)
        AddText(fs, "text")
        AddText(fs, Environment.NewLine & "and this is on a new line")
        Dim i As Integer
        For i = 1 To 100
            AddText(fs, Convert.ToChar(i).ToString())
        Next
        fs.Close()
        fs = File.OpenRead(path)
        Dim b(1024) As Byte
        Dim temp As UTF8Encoding = New UTF8Encoding(True)
        Do While fs.Read(b, 0, b.Length) > 0
            Console.WriteLine(temp.GetString(b))
        Loop
        fs.Close()
    End Sub

    Private Shared Sub AddText(ByVal fs As FileStream, ByVal value As String)
        Dim info As Byte() = New UTF8Encoding(True).GetBytes(value)
        fs.Write(info, 0, info.Length)
    End Sub
End Class

   
  








Related examples in the same category

1.Create Text FileCreate Text File
2.Append String to a Text file
3.Text file Reader and WriterText file Reader and Writer
4.Reading and Writing to Files
5.Write Text to a File
6.Write Text to a File
7.Write text line by line
8.Write Text to a File and read back
9.Open and Append to a Log File
10.Write UTF8 encoded string to a file
11.Create Text file and append string to it
12.Save character to a file
13.Save string to a file