Last updated on October 7th, 2022 at 02:16 pm Show
Streams in C# allow you to transfer data from one point to another quickly and efficiently. The data transfer can be between files, sockets, objects, or even other streams. StreamWriter in C#The C# StreamWriter class is used to write characters to a stream. It is derived from the Abstract class “TextReader” and provides overloaded methods for writing data to files. // StreamWriter class implements a TextWriter for writing characters // to a stream in a particular encoding. public class StreamWriter : System.IO.TextWriter The StreamWriter provides the following useful writing methods.
Example: using System; using System.IO; public class StreamWriterExample { public static void Main(string[] args) { string fileName = @"C:\Temp\textFile.txt"; //Create object of FileInfo for specified path FileInfo fileInfo = new FileInfo(fileName); // Create a FileStream FileStream fileStream = fileInfo.Open(FileMode.OpenOrCreate, FileAccess.Write, FileShare.Read); // Create a StreamWriter using FileStream using (StreamWriter streamWriter = new StreamWriter(fileStream)) { streamWriter.WriteLine("Hello World"); streamWriter.Close(); } Console.ReadLine(); } // Output: Hello World } Once you run the application, you will get the following result in the text file. Output: C# StreamwriterC# Stream ReaderYou can use the StreamReader class In order to read a physical file in C#. The following example will demonstrate to you how to read a file using StreamReader. StreamReader Class Implements a TextReader that reads characters from a byte stream in a particular encoding. public class StreamReader : System.IO.TextReader Example: using System; using System.IO; public class StreamReaderExample { public static void Main(string[] args) { // File name string fileName = @"C:\Temp\textFile.txt"; //Creating an object of FileInfo class for specified path FileInfo fileInfo = new FileInfo(fileName); //Open a file for Read\Write operation using (FileStream fileStream = fileInfo.Open(FileMode.OpenOrCreate, FileAccess.Read, FileShare.Read)) { using(StreamReader streamReader = new StreamReader(fileStream)) { //Use the ReadToEnd method to read all the content from file string text = streamReader.ReadToEnd(); Console.WriteLine(text); Console.ReadLine(); } } } // Output: Hello World } FAQs: StreamReader and StreamWriterQ: What do stream reader and stream writer mean in C#?Ans: The StreamReader and StreamWriter classes are used to read and write data to text files. These classes are derived from an abstract parent class Stream, which facilitates the ability to read from and write to file streams. Q: Does StreamReader provide thread safety?Ans: A StreamReader is not by default thread-safe. Q: What is the use of FileStream class in C#?Ans:
The FileStream is a class used for reading and writing files in C#. It belongs to the System.IO namespace. Q: What exactly do System.IO classes used for?Ans: The System.IO namespace has various classes for creating, removing, reading from, or writing to files, closing files, and other file-related activities. Q: What is the FileStream buffer size?Ans: The default buffer size for the FileStream object is 8192 bytes. Q: Does the StreamWriter class create files in C#?Ans: The StreamWriter constructor initializes a new instance of the StreamWriter class for the given file using the default encoding and buffer size. If the file already exists, it can be either overwritten or appended. The constructor creates a new file if the specified file does not already exist. Q: How do I read a text file in C#?Ans: The File.ReadAllText() method opens a text file, reads every text into a string, and then closes the text file. Q: What is the difference between a StreamReader and StreamWriter in C#?Ans: A StreamReader is used when data has to be read from a file. On the other hand, a StreamWriter is used when data needs to be written to a file. Q: What is the buffer size for the StreamWriter object in C#?Ans: The default buffer size is 1024 characters for the StreamWriter object in C#. Summary:In C#, The System.IO namespace provides various classes and methods that enable developers to perform read-write operations on different files.
References: MSDN- StreamWriter Class , StreamReader Class I hope you enjoyed reading this article “StreamReader and StreamWriter in C#“. If you find something incorrect or wish to offer more information regarding the subjects discussed here, please leave a comment below. Articles to Check Out:
What is StreamReader used for?StreamReader is designed for character input in a particular encoding, whereas the Stream class is designed for byte input and output. Use StreamReader for reading lines of information from a standard text file.
How do I use StreamReader in C sharp?C# StreamReader example to read one line. using System;. using System.IO;. public class StreamReaderExample.. public static void Main(string[] args). FileStream f = new FileStream("e:\\output.txt", FileMode.OpenOrCreate);. StreamReader s = new StreamReader(f);. How do I read a line from a text file in C#?The ReadLine method of StreamReader reads one line at a time.. // Read file using StreamReader. Reads file line by line.. using(StreamReader file = new StreamReader(textFile)) {. int counter = 0;. string ln;. while ((ln = file.ReadLine()) != null) {. Console.WriteLine(ln);. counter++;. What is StreamReader in Visual Basic?StreamReader handles text files. We read a text file in VB.NET by Using StreamReader. This will correctly dispose of system resources and make code simpler. The best way to use StreamReader requires some special syntax.
|