StringReader and StringWriter Classes in VB.NET

In this article I will explain you about StringReader and StringWriter Classes in VB.NET.
  • 10633
 

The StringReader and StringWriter classes, also derived from TextReader and TextWriter, are mainly used to manipulate strings rather than files. The StringReader class is built from a string and provides methods Read and ReadLine to read parts of that string. The StringWriter class is used to write to a StringBuilder class. T he StringBuilder class is used to build a string efficiently. StringWriter provides methods like Write and WriteLine to write to the StringBuilder object. Use these streams if you are dealing with many string manipulations (e.g., a text-to-HTML parser). The example of StringReader and StringWriter class is given below.
 
Example of StringReader and StringWriter Class


    Imports System.IO
    Imports System.Text

        Public
Class StringRW
            Private
sb As New StringBuilder()
            Public Sub New()
            'Call the Writer Method
            Writer()
 
           'Call the Reader Method
            Reader()
        End Sub

        Shared
Sub Main()
            Dim srw As New StringRW()
        End Sub
       
        'Writer Method
        Private Sub Writer()
            Dim sw As New StringWriter(sb)
            Console.WriteLine("Welcome to the Profile Program")
            Console.Write("Name :")

            'Get the name from the console
            Dim name As String = Console.ReadLine()

            'Write to StringBuilder
            sw.WriteLine("Name :" & name)
            Console.Write("Country :")
            Dim country As String = Console.ReadLine()

            'Write to StringBuilder
            sw.WriteLine("Country :" & country)
            Console.Write("Age :")
            Dim age As String = Console.ReadLine()

            'Write to StringBuilder
            sw.WriteLine("Age :" & age)
            Console.WriteLine("Thank You")
            Console.WriteLine("Information Saved!")
            Console.WriteLine()

            'Close the stream
            sw.Flush()
            sw.Close()
            Console.ReadLine()
        End Sub

        Private
Sub Reader()
            Dim sr As New StringReader(sb.ToString())
            Console.WriteLine("Reading Profile")
          
             'Peek to see if the next character exists
            While sr.Peek() > -1
                'Read a line from the string and display it on the
                'console
                Console.WriteLine(sr.ReadLine())
            End While
           
            Console
.WriteLine("Data Read Complete!")
            'Close the string
            sr.Close()
            Console.ReadLine()
        End Sub
    End
Class

Output

srandsr 1.gif
 

This example uses a StringBuilder object to store the input from the user in the Writer method using the StringWriter class. Later, in the Reader method, the example uses the StringReader class to read from the string, which was built in the Writer method.

Primitive Type Stream

Primitive type streams serialize the primitive data types into their native formats. These streams are used when you want to store and retrieve data directly in the form of primitive data types like integer, double etc.

Conclusion

Hope this article would have helped you in understanding StringReader and StringWriter Classes in VB.NET

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.