Recommended Books

VB.NET File Handling



This article covers the use of File Handling in VB.NET.

What is a File? A file is a collection of data with a unique name and a directory (file) path. For File Handling in VB.NET you need to understand the concept of streams. When a file is accessed, either for reading or writing, a stream is created. A stream is a sequence of data, or sometimes it’s also known as a sequence of bytes.

VB.NET gives us two different types of streams, the StreamWriter and the StreamReader. The StreamWriter is used when writing data to the file, and StreamReader is used when reading data from the file. These two are classes which inherit from a common parent class, Stream.


StreamReader

We’ll discuss an example of StreamReader being used to read data from a file we’ve created. We’ve already placed some dummy data into the file, which is shown below.

This guide explains how to use File 
Handling VB.NET through the use of
SteamWriter and StreamReader. These two 
are use to write to, and read from files
respectively.

Onto the code. System.IO is the name of the module these Classes belong to. Importing it will spare you from having to write, System.IO.StreamReader or System.IO.StreamWriter.

Imports System.IO
Module Module1
    Sub Main()

        Dim pointer As StreamReader = New StreamReader("C:/Desktop/FileData.txt")
        Dim line As String

        line = pointer.ReadLine()
        While (line <> Nothing)
            Console.WriteLine(line)
            line = pointer.ReadLine()
        End While

        pointer.Close()

    End Sub
End Module

We declare pointer as object of the StreamReader class, and proceed to use it to perform operations on the file we have selected. Every time pointer.ReadLine() is called, we sequentially move to the next line in the file. The while loop will ensure that they loop keeps repeating until the file has no more lines in it, because once the ReadLine() function hits a blank line, it will return Nothing.

The above code will print out to console, word for word, line for line, the data that was stored in the File. (The output is the exact same as the data shown above, so we won’t show it twice.)

Remember to close the pointer object once you are done. This frees up the memory that was allocated to it, allowing it to be used elsewhere. Check the bottom of this page for an alternative way of automating the close process.


StreamWriter

We’ll discuss an example of StreamReader being used to read data from a file we’ve created. This time we’ll be inserting data into a file, and then reading and displaying it to make sure the data was inserted correctly.

Imports System.IO

Module Module1
    Sub Main()

        Dim wpointer As StreamWriter = New StreamWriter("C:/Desktop/FileData.txt")
        Dim line As String

        wpointer.WriteLine("Coders Legacy")
        wpointer.WriteLine("VB.NET")
        wpointer.WriteLine("File Handling")

        wpointer.Close()

        Dim rpointer As StreamReader = New StreamReader("C:/Desktop/FileData.txt")
        line = rpointer.ReadLine()
        While (line <> Nothing)
            Console.WriteLine(line)
            line = rpointer.ReadLine()
        End While

        rpointer.Close()

    End Sub
End Module

Using the WriteLine() function on the wpointer is an easy way of writing data per line to the File. You could also using the Write() Function as an alternative, find out more about the difference here.

Below is the output of the FileData.txt file.

Coders Legacy
VB.NET
File Handling

Remember, only one Stream can be opened at a time. Hence, in the example above, we first wrote to the file (opening a stream in the process) and then closed the Stream. Only then did we open the Read Stream.

Tip

You can swap out the Dim wpointer As StreamWriter = New StreamWriter("File_path") line for Using wpointer As StreamWriter = New StreamWriter("File_path"). This will have no effect on anything, save for the fact that it will automatically close the object once you are done. The only difference is between the keywords Dim and Using.


This marks the end of the VB.NET File Handling Article. Any suggestions or contributions for CodersLegacy are more than welcome. Any questions can be asked in the comments section below.

Leave a Reply

Your email address will not be published. Required fields are marked *