Reading from and Writing to Text Files

The StreamReader and StreamWriter classes are used for reading from and writing data to text files. These classes inherit from the abstract base class Stream, which supports reading and writing bytes into a file stream.
The StreamReader class also inherits from the abstract base class TextReader that represents a reader for reading series of characters. The following table describes some of the commonly used methods of the StreamReader class −
Public Overrides Sub Close
It closes the StreamReader object and the underlying stream and releases any system resources associated with the reader.
Public Overrides Function Peek As Integer
Returns the next available character but does not consume it.
Public Overrides Function Read As Integer
Reads the next character from the input stream and advances the character position by one character.
The following example demonstrates reading a text file named Jamaica.txt. The file reads −
Down the way where the nights are gay
And the sun shines daily on the mountain top
I took a trip on a sailing ship
And when I reached Jamaica
I made a stop
Imports System.IO
Module fileProg
   Sub Main()
      Try
         ' Create an instance of StreamReader to read from a file. 
         ' The using statement also closes the StreamReader. 
         Using sr As StreamReader = New StreamReader("e:/jamaica.txt")
            Dim line As String
            ' Read and display lines from the file until the end of  
            ' the file is reached. 
            line = sr.ReadLine()
            While (line <> Nothing)
               Console.WriteLine(line)
               line = sr.ReadLine()
            End While
         End Using
      Catch e As Exception
         ' Let the user know what went wrong.
         Console.WriteLine("The file could not be read:")
         Console.WriteLine(e.Message)
      End Try
      Console.ReadKey()
   End Sub
End Module
Guess what it displays when you compile and run the program!
The StreamWriter class inherits from the abstract class TextWriter that represents a writer, which can write a series of character.
The following table shows some of the most commonly used methods of this class −
Public Overrides Sub Close
Closes the current StreamWriter object and the underlying stream.
Public Overrides Sub Flush
Clears all buffers for the current writer and causes any buffered data to be written to the underlying stream.
Public Overridable Sub Write (value As Boolean)
Writes the text representation of a Boolean value to the text string or stream. (Inherited from TextWriter.)
Public Overrides Sub Write (value As Char)
Writes a character to the stream.
Public Overridable Sub Write (value As Decimal)
Writes the text representation of a decimal value to the text string or stream.
Public Overridable Sub Write (value As Double)
Writes the text representation of an 8-byte floating-point value to the text string or stream.
Public Overridable Sub Write (value As Integer)
Writes the text representation of a 4-byte signed integer to the text string or stream.
Public Overrides Sub Write (value As String)
Writes a string to the stream.
Public Overridable Sub WriteLine
Writes a line terminator to the text string or stream.
The above list is not exhaustive. For complete list of methods please visit Microsoft's documentation
The following example demonstrates writing text data into a file using the StreamWriter class −
Imports System.IO
Module fileProg
   Sub Main()
      Dim names As String() = New String() {"Zara Ali", _
         "Nuha Ali", "Amir Sohel", "M Amlan"}
      Dim s As String
      Using sw As StreamWriter = New StreamWriter("names.txt")
         For Each s In names
            sw.WriteLine(s)
         Next s
      End Using
      ' Read and show each line from the file. 
      Dim line As String
      Using sr As StreamReader = New StreamReader("names.txt")
         line = sr.ReadLine()
         While (line <> Nothing)
            Console.WriteLine(line)
            line = sr.ReadLine()
         End While
      End Using
      Console.ReadKey()
   End Sub
End Module
When the above code is compiled and executed, it produces the following result −
Zara Ali
Nuha Ali
Amir Sohel
M Amlan 


    63 Lectures 
     4 hours 

 Frahaan Hussain

    103 Lectures 
     12 hours 

 Arnold Higuit

    60 Lectures 
     9.5 hours 

 Arnold Higuit

    97 Lectures 
     9 hours 

 Arnold Higuit
 Print
 Add Notes
 Bookmark this page