ASP.NET

StreamReader And StreamWriter In Asp.Net

StreamReader And StreamWriter In Asp.Net

StreamReader And StreamWriter In Asp.Net

StreamReader and StreamWriter are found in the System.IO namespace.
Both classes are used for reading from and writing data to text files. These classes inherit from the abstract base class "TextReader" and "TextWriter", which supports reading and writing bytes into a file stream.

Properties and Methods of StreamWriter class.
Close() - Closes the current StreamWriter object and the underlying stream. This method is equivalent to Dispose(), that is used to release resources.
Write() - Write method is used to write data to a text stream without a newline.
WriteLine() - WriteLine method is used to write data to a text stream with a newline.
Flush() - This method Clears data from all buffers for the current writer and causes any buffered data to be written to the underlying stream.
Dispose() - It releases the unmanaged resources used by the StreamWriter and optionally releases the managed resources.

AutoFlush Property - Gets or sets a value indicating whether the StreamWriter will flush its buffer to the underlying stream after every call to System.IO.StreamWriter.Write(System.Char).
BaseStream Property - Gets the underlying stream that interfaces with a backing store.
Encoding Property - Gets the System.Text.Encoding in which the output is written.



Properties and Methods of StreamReader class.
Close() - Closes the current StreamReader object and the underlying stream. This method is equivalent to Dispose(), that is used to release resources.
Read() - Reads the next character from the input stream.
ReadLine() - Reads a line of characters from the current stream and returns the data as a string.
ReadToEnd()   - Reads the stream from the current position to the end of the stream.


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 (StreamReader sr = new StreamReader(f))
        {
            string line = "";
            while ((line = sr.ReadLine()!) != null)
            {
                Console.WriteLine("{0}", line);
            }
        }
    }

    public static void WriteToFile()
    {
        string f = @"D:\Practice\practice.txt";
        using (StreamWriter sw = new StreamWriter(f))
        {
            sw.WriteLine("Here are details");
            sw.WriteLine("");
            sw.WriteLine("Welcome to TheTechfoyer.com");
            sw.WriteLine("File Handling in C#");
            sw.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