SUMMARY
This article demonstrates how to retrieve information from
a text file and use an
ArrayList class to display that information to the user.
Reading text files in Visual Basic 2005 or in Visual Basic .NET
Opening and reading files for read access is an important part of
input/output (IO) functionality, even if you do not need to write to the file
in question. This example opens a file for reading, which is useful for text
files but is inappropriate for binary files. This example uses one of many
methods that are available for opening the file. Although many data structures
are available to store information that is retrieved from the file, an
ArrayList class is the easiest structure to use. To open and read from the
file, this example uses objects from the
System.IO namespace, specifically the
System.IO.StreamReader class.
Note This sample requires some form of a text (.txt) file from which
to read.
To load and read a text file from Microsoft Visual Basic 2005 or from Microsoft Visual Basic .NET, follow
these steps:
- Start Microsoft Visual Studio 2005 or Microsoft Visual Studio .NET. Create a new Console Application
in Visual Basic. Visual Studio creates a Module for you, along with an empty Main() procedure.
- Make sure that the project references at least the System namespace. Use the Imports statement on the System, System.IO, and System.Collections namespaces so you are not required to qualify declarations from
these namespaces later in your code. You must use these statements prior to any
other declarations.
Imports System
Imports System.IO
Imports System.Collections
- To open a file for reading, create a new instance of a StreamReader object, and pass the file's path into the constructor as follows:
Dim objReader As New StreamReader("c:\test.txt")
- You will need a string variable in which to store each line
of the file as you process. Because you will be adding these lines to an ArrayList, declare and create an object of that type as well.
Dim sLine As String = ""
Dim arrText As New ArrayList()
- There are several ways to read the file in, including the ReadToEnd method which reads in the entire file at once. However, for this
example, you can use the ReadLine method to bring in the file one line at a time. When the end of
the file is reached, this method returns "Nothing," which allows a way to end
your loop. As you read each line from the file, you can use the Add method of the ArrayList to insert the lines into your ArrayList class.
Do
sLine = objReader.ReadLine()
If Not sLine Is Nothing Then
arrText.Add(sLine)
End If
Loop Until sLine Is Nothing
objReader.Close()
- Use a "For Each" loop to write the contents of your newly
filled ArrayList to the console as follows:
For Each sLine In arrText
Console.WriteLine(sLine)
Next
Console.ReadLine()
- Save and run your code, which produces a listing of your
file to the console.
Complete code listing
Imports System
Imports System.IO
Imports System.Collections
Module Module1
Sub Main()
Dim objReader As New StreamReader("c:\test.txt")
Dim sLine As String = ""
Dim arrText As New ArrayList()
Do
sLine = objReader.ReadLine()
If Not sLine Is Nothing Then
arrText.Add(sLine)
End If
Loop Until sLine Is Nothing
objReader.Close()
For Each sLine In arrText
Console.WriteLine(sLine)
Next
Console.ReadLine()
End Sub
End Module
Pitfalls
There are several things to be aware of when you work with file
I/O, including the following items:
- Any time you access a file, there is the possibility that
the file that you are trying to read or write may not be on the system or may
be in use.
- This example reads the entire file into memory before it
processes the file. You may encounter a situation in which the file is too
large to be held in memory, or you may not have permissions to access the
file.
Any of these situations cause an exception to be raised. It is
always good practice to provide a
try...catch block to handle these common issues.
REFERENCES
For more information, see the following Microsoft .NET SDK
QuickStart Tutorials Web site:
For more general information about Visual Basic .NET, see the
following Usenet newsgroup: