Home
VB.NET
StreamWriter Example
This page was last reviewed on Nov 10, 2023.
Dot Net Perls
StreamWriter. This writes data to a text file. It can write lines of text or strings to the file. It is easy to use, and one of the most common classes in VB.NET programs.
Ideal usage. Ideally StreamWriter should be used inside a VB.NET Using statement. This ensures the correct disposal of its resources. The syntax is clear and simple.
StreamReader
File
Example. Here we use StreamWriter to create a new file, and then write 2 lines of text to it along with 2 newlines. Note how the System.IO namespace is imported at the top.
Start The StreamWriter is instantiated and the file "myfile.txt" is opened for writing. You can change the path to another location.
Next The Write call and the two WriteLine calls write the three Strings to the file. The file's contents are listed in the comment.
Imports System.IO Module Module1 Sub Main() Using writer As StreamWriter = New StreamWriter("myfile.txt") writer.Write("One ") writer.WriteLine("two 2") writer.WriteLine("Three") End Using End Sub End Module
One two 2 Three
Append text. Here we use StreamWriter to append lines of text to a file. This is more efficient than reading in the entire file—only the last part of the file needs to be accessed.
Argument 1 The first part of the example opens the file at the absolute path "C:\append.txt".
Argument 2 The second argument to the StreamWriter constructor is the Boolean True. This specifies the append overload.
Then The file is opened again, and a second line is appended to it. The text file contents are shown in the final comment.
Imports System.IO Module Module1 Sub Main() ' Append the first line of text to a new file. Using writer As StreamWriter = New StreamWriter("C:\append.txt", True) writer.WriteLine("First line appended; 1") End Using ' Append the second line. Using writer As StreamWriter = New StreamWriter("C:\append.txt", True) writer.WriteLine("Second line appended; 2") End Using End Sub End Module
First line appended; 1 Second line appended; 2
For-loop. Here we use a For-loop with StreamWriter. When you have to loop and add text to a file, you should reuse a single StreamWriter—this improves performance.
For
Start The file is opened, and one Windows file handle is created. Then, that same system resource is used to write ten numbers to the file.
Next The Write call in the For-loop specifies a format string. The format string prints the Integer with one decimal place.
Integer
Imports System.IO Module Module1 Sub Main() ' Create new file. Using writer As StreamWriter = New StreamWriter("loop.txt") ' Loop through ten numbers. Dim i As Integer For i = 0 To 10 - 1 ' Write number with format string. writer.Write("{0:0.0} ", i) Next i End Using End Sub End Module
0.0 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0
Summary. Here we saw examples of using StreamWriter in VB.NET. This is an easy way to write to files in the .NET platform. The Using statement is an essential part of the type.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Nov 10, 2023 (simplify).
Home
Changes
© 2007-2024 Sam Allen.