VB .Net

Sequential File

Last revised: December 11, 2003 .

This page shows how to set a sequential file without using MS Flex Grid. To see how you can set up a sequential file using MS Flex Grid, go to the Sequential File with MS Flex Grid page.

Using a sequential file, an application can allow users to store data into a text file for permanent storage. It also allows an application to read the data from the text file.

The data is written and read from a text file in a sequential order, and that is why it is called sequential file.

For example, you have a list of persons' names including Pat, Tom, and Sue to write into a sequential file. VB .Net uses the StreamWriter class to carry out the operation.

  1. The StreamWriter of VB .Net will write Pat into the file, so Pat is the first record.
  2. Then the StreamWriter will write Tom into the file, so Tom will be the second record after Pat.
  3. Lastly, the StreamWriter will write Sue to the file, so Sue will be the third, Tom will stay at the second, and Sue will stay at the first.

The diagram below shows how the status of data change as the StreamWriter writes each name into the sequential file.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

There are three basic operations in sequential files.

  1. Creating and Writing data into a file
  2. Appending or adding data into an existing file
  3. Reading data from an existing file

The following instruction will use a sample application to explain the concepts of sequential file.

Create and Write to a Sequential File

To create a sequential file, you use StreamWriter class to instantiate an object variable. In other words, to create a sequential file, you need to define a representation of the StreamWriter class. And the object variable in the statement below is a representation or instantiation of the StreamWriter, and when you refer to the StreamWriter, you use the object variable.

The basic format of instantiation is:

Dim objectVariable As System.IO.StreamWriter

, where System.IO.StreamWriter is the StreamWriter class which is a part of the System class that comes with VB .Net and objectVariable is the name of instance that you create.

Let's say, you want to create a instance of StreamWriter for a number of products in your inventory. The following code will instantiate the StreamWriter.

Dim productsFile As System.IO.StreamWriter

When you refer to the StreamWriter class, use productsFile object variable name.

The next step is creating an actual text file that is linked to the object variable. The basic format is:

objectVariable = System.IO.File.CreateText("file_address_and_name")

In our example, the code is:

productsFile = System.IO.File.CreateText("products.txt")

In summary, you will need two statements to create a text file to store data:

Dim objectVariable As System.IO.StreamWriter

objectVariable = System.IO.File.CreateText("file_address_and_name")

In the example, we have:

Dim productsFile As System.IO.StreamWriter

productsFile = System.IO.File.CreateText("products.txt")

Yun's Advice 1: Creating a new text file to store data should happen only once, otherwise the existing file will be replaced when a new file under the same data file name is created. Effectively, the data in the old file will be deleted. To avoid accidental re-creation of the data file, be sure to check if the file is already existing before creating one. Here is a sample code:

Dim productsFile As System.IO.StreamWriter
If System.IO.File.Exists("products.txt") Then
     MsgBox("The products.txt file is already existing", MsgBoxStyle.OKOnly)
Else
     productsFile = System.IO.File.CreateText("products.txt")
End If

 

Once the text file is created, you can write data into the text file using the StreamWriter object. The basic format is:

objectVariable.WriteLine(data)

In our example, the codes are:

productsFile.WriteLine(txtItemNumber.Text)
productsFile.WriteLine(txtDescription.Text)
productsFile.WriteLine(txtPrice.Text)

Yun's Advice 2: Although, the StreamWriter class allows you to create a file and write data into the file in a same module, it is a good idea to separate them in two modules for simplicity.

Yun's Advice 3: Don't forget to close the object variable. As long as it is open, other parts of the application can not access to the file. The basic format is:

VariableName.Close( )

or in our example, it is

productsFile.Close( )

 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

 

Appending or Adding Data into an Existing File

Once a sequential text file is created and closed, you can reopen it and add or append more data to it.

If you instantiated the VariableObject as a global variable or declared it in the same module, then you do not need to re-instantiate it. However, If you instantiated the VariableObject as a local variable and you are appending in the file in separate module, then you do need to re-instantiate the VariableObject as:
        Dim objectVariable As System.IO.StreamWriter
or in our example, we have:
        Dim productsFile As System.IO.StreamWriter

To append an existing file, you use AppendText method. The basic format is the following:

objectVariable = System.IO.File.AppendText("file_address_and_name")

In our example, we have:

productsFile = System.IO.File.AppendText("products.txt")

As your objectVariable is linked to the text file, you can append it using WriteLine method.

The basic format is:

objectVariable.WriteLine(data)

In our example, the codes are:

productsFile.WriteLine(txtItemNumber.Text)
productsFile.WriteLine(txtDescription.Text)
productsFile.WriteLine(txtPrice.Text)

If txtItemNumber.Text was "101" and txtDescription.Text was "Computer Set" and txtPrice.Text was "2500", the text file will store the data as:

As more data are appended, the text file can be:

Once appending is complete, be sure to close the object variable. As long as it is open, other parts of the application can not access to the file. The basic format is VariableName.Close( ) or in our example, it is productsFile.Close( )

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Reading Data from an Existing File

To read data from existing file requires an instance of StreamReader class. The StreamReader class is similar to StreamWriter except it reads instead.

The basic format of instantiation is:

Dim objectVariable As System.IO.StreamReader

In our example application, the following code will instantiate the StreamReader.

Dim productsFile As System.IO.StreamReader

The next step is assigning an address of the text file to the objectVariable. The basic format is:

objectVariable = System.IO.File.OpenText("file_address_and_name")

In our example application, the following code will assign the address of products.txt to productsFile:

productsFile = System.IO.File.OpenText("products.txt")

In summary, you have two lines of codes for instantiating an objectVariable and assigning an address of a text file to it.

Dim objectVariable As System.IO.StreamReader

objectVariable = System.IO.File.OpenText("file_address_and_name")

In the example, we have:

Dim productsFile As System.IO.StreamReader

productsFile = System.IO.File.OpenText("products.txt")

To read each line from the sequential file, use the ReadLine method. The basic format is

Variable = objectVariable.ReadLine( )

In our example, the code is

strEachProduct = productsFile.ReadLine( )

For most cases, you will read mulple line of data, so using a loop would make sense. For example:

     Do Until productsFile.Peek = -1
          'Read in three lines from the text file that are Product ID,
          'Description, and Price into strEachProduct variable.

          For intCounter = 1 To 3
               strEachProduct &= productsFile.ReadLine( ) & Space(10)
          Next

          'Display each product in the list box.
          lstDisplay.Items.Add(strEachProduct)

          'Re-initialize the variable for the next product.
          strEachProduct = ""
     Loop

Here, the application will read the data until it sees a blank line. The Peek method allows the application to peek one line ahead of the ReadLine method.

Once reading data is complete, be sure to close the object variable. As long as it is open, other parts of the application can not access to the file. The basic format is VariableName.Close( ) or in our example, it is productsFile.Close( )

Here is the codes from the example application.

'Check if the products.txt file is already existing.
If System.IO.File.Exists("products.txt") Then
     'Opens the products.txt file. It assigns the address of the products.txt file
     'in productsFile object.

     productsFile = System.IO.File.OpenText("products.txt")

     'Read each product item until the end of the products.txt file.
     'The Peek method returns the next character to be read,
     '    or -1 if no more characters are available.

     Do Until productsFile.Peek = -1
          'Read in three lines from the text file that are Product ID,
          'Description, and Price into strEachProduct variable.

          For intCounter = 1 To 3
               strEachProduct &= productsFile.ReadLine( ) & Space(10)
          Next

          'Display each product in the list box.
          lstDisplay.Items.Add(strEachProduct)

          'Re-initialize the variable for the next product.
          strEachProduct = ""
     Loop

     'Close the productsFile.
     productsFile.Close()
Else
     'If no matching file is found, display a message.
     MsgBox("There is no file to read.", MsgBoxStyle.OKOnly)
End If

 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

You may view the code of the example form. Or you may download and unzip the entire project.

Also, here is a similar applications using a sequential file and an array.