Se lo StreamWriter è troppo lento

Generalmente per salvare un file di testo è molto comodo utilizzare uno StreamWriter:

            Dim sw As New IO.StreamWriter("C:\SW prova.txt")
            sw.Write("Mio testo")
            sw.Flush()
            sw.Close()

Se il testo da salvare però è di grandi dimensioni, lo StreamWriter sarà lento ad eseguire la scrittura su disco. Per evitare rallentamenti è possibile salvare il file come flusso di byte utilizzando la classe IO.FileStream:

            Dim fs As New IO.FileStream("C:\FS prova.txt", IO.FileMode.OpenOrCreate)
            Dim enc As New System.Text.UTF8Encoding
            Dim bytes() As Byte = enc.GetBytes("Mio testo molto lungo")
            fs.Write(bytes, 0, bytes.Length)
            fs.Flush()
            fs.Close()

 

Tag Cloud:

Print | posted on venerdì 23 ottobre 2009 9.48

Feedback

No comments posted yet.

Your comment:





 
Please add 7 and 8 and type the answer here:

Copyright © Andrea Zingoni

Design by Bartosz Brzezinski

Design by Phil Haack Based On A Design By Bartosz Brzezinski