SUMMARY
This article demonstrates how to read from and write to a
text file by using Microsoft Visual Basic 2005 or Microsoft Visual Basic .NET.
Requirements
The following list outlines the recommended hardware, software, network infrastructure, and service packs that you need:
- Visual Basic 2005 or Visual Basic .NET
This article assumes that you are familiar with the following topics:
- Visual Basic 2005 or Visual Basic .NET
Read and write text files
The "
Read a text file"
section demonstrates how to use the
StreamReader object to read a text file. The "
Write a text file (example 1)" section and the
"
Write a text file (example 2)"
section demonstrate how to use the
StreamWriter class to write text to a file.
Read a text file
The following code uses the
StreamReader class to open, to read, and to close the text file. You can pass
the path name of a text file to the
StreamReader constructor to open the file automatically. The
ReadLine method reads each line of text and increments the file pointer to
the next line as it reads. When the
ReadLine method reaches the end of the file, it returns
Nothing.
- Start Microsoft Visual Studio 2005 or Microsoft Visual Studio .NET.
- Create a new Console Application in Visual Basic 2005 or Visual Basic
.NET.
- Add the following code to the top of the Module1.vb file:
Imports System.IO
- Add the following code to the Sub Main procedure.
Dim objStreamReader As StreamReader
Dim strLine As String
'Pass the file path and the file name to the StreamReader constructor.
objStreamReader = New StreamReader("C:\Boot.ini")
'Read the first line of text.
strLine = objStreamReader.ReadLine
'Continue to read until you reach the end of the file.
Do While Not strLine Is Nothing
'Write the line to the Console window.
Console.WriteLine(strLine)
'Read the next line.
strLine = objStreamReader.ReadLine
Loop
'Close the file.
objStreamReader.Close()
Console.ReadLine()
- On the Debug menu, click Start to compile and to run the application. Press ENTER to close the
Console window.
The Console window displays the Boot.ini file. Note
that the contents of the Boot.ini file may vary from configuration to
configuration. The following output is a sample Boot.ini file:
[boot loader]
timeout=5
default=multi(0)disk(0)rdisk(0)partition(1)\WINNT
[operating systems]
multi(0)disk(0)rdisk(0)partition(1)\WINNT="Windows 2000 Professional" /fastdetect
C:\ = "Windows 98"
Write a text file (example 1)
The following code uses the
StreamWriter class to open, to write to, and to close the text file. Similar
to
StreamReader, you can pass the path name of a text file to the
StreamWriter constructor to open the file automatically. The
WriteLine method writes a complete line of text to the text file.
- Start Visual Studio 2005 or Visual Studio .NET.
- Create a new Console Application in Visual Basic 2005 or Visual Basic
.NET.
- Add the following code to the top of Module1.vb:
Imports System.IO
- Add the following code to the Sub Main procedure:
Dim objStreamWriter As StreamWriter
'Pass the file path and the file name to the StreamWriter constructor.
objStreamWriter = New StreamWriter("C:\Testfile.txt")
'Write a line of text.
objStreamWriter.WriteLine("Hello World")
'Write a second line of text.
objStreamWriter.WriteLine("From the StreamWriter class")
'Close the file.
objStreamWriter.Close()
- On the Debug menu, click Start to compile and to run the application.
This code
creates a file named Test.txt on drive C. Open Test.txt in a text editor such
as Notepad. Test.txt contains two lines of text:
Hello World
From the StreamWriter class
Write a text file (example 2)
The following code uses the
StreamWriter class to open, to write to, and to close the text file. Unlike
the previous example, this code passes two additional parameters to the
constructor.
The first parameter is the file path and file name of
the file. The second parameter,
True, specifies that the file be opened in append mode. If you specify
False for the second parameter, the contents of the file are
overwritten each time you run the code. The third parameter specifies
Unicode so that
StreamWriter encodes the file in Unicode. You can also specify the following
encoding methods for the third parameter:
- ASCII
- BigEndianUnicode
- UTF7
- UTF8
The
Write method is similar to the
WriteLine method except that
Write does not automatically embed a carriage return/line feed (CR/LF)
character combination. This is useful when you want to write one character at a
time.
- Start Visual Studio 2005 or Visual Studio .NET.
- Create a new Console Application in Visual Basic 2005 or Visual Basic
.NET.
- Add the following code to the top of Module1.vb:
Imports System.IO
Imports System.Text
- Add the following code to the Sub Main procedure under your existing code:
Dim objStreamWriter As StreamWriter
Dim x As Long
'Open the file.
objStreamWriter = New StreamWriter("C:\Test2.txt", True, _
Encoding.Unicode)
'Write out the numbers 1 through 10 on the same line.
For x = 1 To 10
objStreamWriter.Write(x)
Next x
'Close the file.
objStreamWriter.Close()
- On the Debug menu, click Start to compile and to run the application.
This code
creates a file that is named Test2.txt on drive C. Open Test2.txt in a text editor such
as Notepad. Test2.txt contains a single line of text:
12345678910
Note If you run the code several times, the "123456789" text is
repeated on the same line.
Complete code listing
'Read a Text File
Imports System.IO
Module Module1
Sub Main()
Dim objStreamReader As StreamReader
Dim strLine As String
'Pass the file path and the file name to the StreamReader constructor.
objStreamReader = New StreamReader("C:\Boot.ini")
'Read the first line of text.
strLine = objStreamReader.ReadLine
'Continue to read until you reach the end of the file.
Do While Not strLine Is Nothing
'Write the line to the Console window.
Console.WriteLine(strLine)
'Read the next line.
strLine = objStreamReader.ReadLine
Loop
'Close the file.
objStreamReader.Close()
Console.ReadLine()
End Sub
End Module
'Write a Text File: Version 1
Imports System.IO
Module Module1
Sub Main()
Dim objStreamWriter As StreamWriter
'Pass the file path and the file name to the StreamWriter constructor.
objStreamWriter = New StreamWriter("C:\Text.txt")
'Write a line of text.
objStreamWriter.WriteLine("Hello World")
'Write a second line of text.
objStreamWriter.WriteLine("From the StreamWriter class")
'Close the file.
objStreamWriter.Close()
End Sub
End Module
'Write a Text File: Version 2
Imports System.IO
Imports System.Text
Module Module1
Sub Main()
Dim objStreamWriter As StreamWriter
Dim x As Long
'Open the file.
objStreamWriter = New StreamWriter("C:\Test2.txt", True, _
Encoding.Unicode)
'Write out the numbers 1 through 10 on the same line.
For x = 1 To 10
objStreamWriter.Write(x)
Next x
'Close the file.
objStreamWriter.Close()
End Sub
End Module
Troubleshooting
Whenever you input or output a file, it is good programming
practice to wrap the code inside a
Try-End-Try block in case an error occurs. Some possible errors include a
file that does not exist, or a file that is already in use.