To Flush or Not to Flush

T

Tom

Do I really need to flush a streamwriter? It appears to work either way.

Dim sw As StreamWriter
Dim strFullPath As String = "C:\Temp\Test.txt"

If File.Exists(strFullPath) Then
sw = File.AppendText(strFullPath)
Else
sw = File.CreateText(strFullPath)
End If

sw.WriteLine("Hello its me")

'sw.Flush()
sw.Close()

Tom
 
D

David Browne

Tom said:
Do I really need to flush a streamwriter? It appears to work either way.

Dim sw As StreamWriter
Dim strFullPath As String = "C:\Temp\Test.txt"

If File.Exists(strFullPath) Then
sw = File.AppendText(strFullPath)
Else
sw = File.CreateText(strFullPath)
End If

sw.WriteLine("Hello its me")

'sw.Flush()
sw.Close()

Close always Flush'es first.

You would only need to flush if you were keeping the StreamWriter open and
wanted to make sure that all of your previous writes were safely on-disk.

David
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top