ASP.NET

Text Reader & Text Writer in Asp.Net

Text Reader & Text Writer in Asp.Net

Text Reader & Text Writer in Asp.Net

The TextReader and TextWriter classes in C# are another way to read and write files respectively, even though these are not stream classes.
In C# the TextWriter class is written as an abstract class. It is used to create a sequential series of characters inside a file.
It is quite similar to the stream writer that also allows the user to write sequential characters or text in a file but it doesn’t require FileStream creation for the operation.

TextWriter Class in C#
The TextWriter class in C# represents a writer that can write sequential series of characters. We can use this TextWriter class to write text in a file. It is an abstract base class of StreamWriter and StringWriter, which write characters to streams and strings respectively. It is used to write text or sequential series of characters into files. It is found in the System.IO namespace

Methods of TextWriter class in C#:
Synchronized(TextWriter): It is used to Create a thread-safe wrapper around the specified TextWriter.
Close(): It Closes the current writer and releases any system resources associated with the writer.
Dispose(): It releases all resources used by the System.IO.TextWriter object.
Flush(): It Clears all buffers for the current writer and causes any buffered data to be written to the underlying device.
Write(Char):
It is used to write a character to the text stream.
Write(String): It is used to write the string to the text stream.
WriteAsync(Char): It is used to write the character to the text stream asynchronously.
WriteLine(): It is used to write a line terminator to the text stream.
WriteLineAsync(String): It is used to write the string to the text stream asynchronously followed by a line terminator.


Points to Remember about TextWriter:
1. TextWriter is an abstract class belonging to the System.IO namespace.
2. It is used to write a sequential series of characters into a file.
3. It is the base class of StreamWriter and StringWriter which is used to write characters to streams and strings respectively.
4. By default, it is not thread-safe.
5. As it is an abstract class, its object cannot be created. Any class implementing TextWriter must minimally implement its Write(Char) method to create its useful instance.


List of derived classes
Like StreamWriter there are also other classes that are derived from the TextWriter class and provide the implementation for the members of TextWriter.
IndentedTextWriter: It is used to insert a tab string and to track the current indentation level.
StreamWriter: It is used to write characters to a stream in a particular encoding.
StringWriter: It is used to write information to a string. The information is stored in an underlying StringBuilder.
HttpWriter: It provides an object of TextWriter class that can be accessed through the intrinsic HttpResponse object.
HtmlTextWriter: It is used to write markup characters and text to an ASP.NET server control output stream.



TextReader class in C#
The TextReader class in C# represents a reader that is used to read text or sequential series of characters from a text file.
TextReader class belongs to the System.IO namespace. It is an abstract class which means you cannot instantiate it.
It is an abstract base class of StreamReader and StringReader which are used to read characters from stream and string respectively.

Methods of TextReader Class in C#:

Synchronized(): It is used to create a thread-safe wrapper around the specified TextReader.
Close(): It is used to close the TextReader and release any system resources associated with the TextReader.
Dispose(): It is used to release all resources used by the TextReader object.
Peek(): It is used to read the next character without changing the state of the reader or the character source. Returns the next available character without actually reading it from the reader. It returns an integer representing the next character to be read, or -1 if no more characters are available or the reader does not support seeking.
Read(): It is used to read the next character from the text reader and advances the character’s position by one character. It returns the next character from the text reader, or -1 if no more characters are available. The default implementation returns -1.
ReadLine(): It is used to read a line of characters from the text reader and returns the data as a string. It returns the next line from the reader, or null if all characters have been read.
ReadToEnd(): It is used to read all characters from the current position to the end of the text reader and returns them as one string. That means it reruns a string that contains all characters from the current position to the end of the text reader.


using System;
using System.IO;

class Program  
{  
    static void Main(string[] args)  
    {              
        // Write to a Text file on your drive
        WriteToFile();  
        // Read from a text file
        ReadFromFile();          
    }  

    public static void ReadFromFile()
    {
        string f = @"D:\Practice\practice.txt";
        //Creating an Instance StreamReader Object to Read the Data from the File Path
        using (TextReader textReader = File.OpenText(f))
        {
            string line = "";
            while ((line = textReader.ReadLine()!) != null)
            {
                Console.WriteLine("{0}", line);
            }
        }
    }

    public static async void WriteToFile()
    {
        string f = @"D:\Practice\practice.txt";
        // It creates or opens a file for writing UTF-8 encoded text.
        using (TextWriter writer = File.CreateText(f))
        {
            writer.WriteLine("Here are details");
            writer.WriteLine("");
            writer.WriteLine("Welcome to DotNetTutorials");
            writer.WriteLine("You are Learning File Handling in C#");
            writer.WriteLine("");
        }
    }
}  




Related Post

About Us

Community of IT Professionals

A Complete IT knowledgebase for any kind of Software Language, Development, Programming, Coding, Designing, Networking, Hardware and Digital Marketing.

Instagram