Streams in C#
Streams are ordinarily utilized when reading information from large records. By utilizing streams, the information from expansive grinds in separated into little lumps and sent to the stream. These pieces of information would then be able to be perused from the application. The purpose behind separating it into little pieces is a direct result of the exhibition effect of perusing a major record in one shot. The best methodology is then to utilize streams to separate the record into sensible lumps. To read text data from a byte stream we implement Stream Reader as well as for writing data we use Stream Writer class to use these classes belong to using System. IO; namespace.so let' understand in detail.
1-Stream Writer
C# Stream Writer class is utilized to compose characters to a stream in explicit encoding. It acquires the TextWriter class. It provide over-burden function like compose() and writeln() strategies to compose information into record.So let's Understand through the program in which we will create a function to write some text as Rohit Programming zone and we will save it as File.txt in the computer f:// drive location and also once the file is created message appears on the user screen, So to perform this task we have to use Stream Writer class which belongs to the System.IO Namespace. simply copy the yellow highlighted code as follows.
Program
using System;
using System.IO;
namespace program
{
class Program
{
static void Main(string[] args)
{
FileStream fs = new FileStream("d:\\File.txt", FileMode.Create);
StreamWriter sw = new StreamWriter(fs);
sw.WriteLine("Rohit Programming zone");
sw.Close();
fs.Close();
Console.WriteLine("File Saved");
Console.ReadKey();
}
}
}
Output
Now go to the f:// Drive section and search for File.txt and then open the file and see the text is saved as Rohit Programming zone.
2-Stream Reader
StreamReader class is utilized to read the string from the stream. It acquires the TextReader class. It provides Read() and ReadLine() techniques to read information from the stream. The information from the record is first perused into the stream. From that point, the application peruses the information from the stream. In the above program, we have learned how we can write the document file using Stream Writer class, Similarly, Now we will create a program with doing some changes in it so simply copy the yellow highlighted code as shown below.
Program
using System;
using System.IO;
namespace program
{
class Program
{
static void Main(string[] args)
{
/*Existing file path*/ FileStream fs = new FileStream("d:\\File.txt", FileMode.Open);
StreamReader sr = new StreamReader(fs);
string line = sr.ReadLine();
Console.WriteLine("Text File Opened");
Console.WriteLine(line);
Console.ReadKey();
}
}
}
Output
Streams are ordinarily utilized when reading information from large records. By utilizing streams, the information from expansive grinds in separated into little lumps and sent to the stream. These pieces of information would then be able to be perused from the application. The purpose behind separating it into little pieces is a direct result of the exhibition effect of perusing a major record in one shot. The best methodology is then to utilize streams to separate the record into sensible lumps. To read text data from a byte stream we implement Stream Reader as well as for writing data we use Stream Writer class to use these classes belong to using System. IO; namespace.so let' understand in detail.
1-Stream Writer
C# Stream Writer class is utilized to compose characters to a stream in explicit encoding. It acquires the TextWriter class. It provide over-burden function like compose() and writeln() strategies to compose information into record.So let's Understand through the program in which we will create a function to write some text as Rohit Programming zone and we will save it as File.txt in the computer f:// drive location and also once the file is created message appears on the user screen, So to perform this task we have to use Stream Writer class which belongs to the System.IO Namespace. simply copy the yellow highlighted code as follows.
Program
using System;
using System.IO;
namespace program
{
class Program
{
static void Main(string[] args)
{
FileStream fs = new FileStream("d:\\File.txt", FileMode.Create);
StreamWriter sw = new StreamWriter(fs);
sw.WriteLine("Rohit Programming zone");
sw.Close();
fs.Close();
Console.WriteLine("File Saved");
Console.ReadKey();
}
}
}
Output
Now go to the f:// Drive section and search for File.txt and then open the file and see the text is saved as Rohit Programming zone.
2-Stream Reader
StreamReader class is utilized to read the string from the stream. It acquires the TextReader class. It provides Read() and ReadLine() techniques to read information from the stream. The information from the record is first perused into the stream. From that point, the application peruses the information from the stream. In the above program, we have learned how we can write the document file using Stream Writer class, Similarly, Now we will create a program with doing some changes in it so simply copy the yellow highlighted code as shown below.
Program
using System;
using System.IO;
namespace program
{
class Program
{
static void Main(string[] args)
{
/*Existing file path*/ FileStream fs = new FileStream("d:\\File.txt", FileMode.Open);
StreamReader sr = new StreamReader(fs);
string line = sr.ReadLine();
Console.WriteLine("Text File Opened");
Console.WriteLine(line);
Console.ReadKey();
}
}
}