Skip to main content

Streams

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

Popular posts from this blog

Media Player in C# Visual Studio By Rohit Programming Zone

Media Player  in C# Visual Studio   Source Code ----------------------------------------------------------------------------------------------------------------------------- using System; using System.Windows.Forms;   namespace Media_Player_in_C_Sharp_By_Rohit_Programming_Zone {     public partial class Form1 : Form     {         public Form1()         {             InitializeComponent();         }         string [] paths, files;           private void btn_open_Click( object sender, EventArgs e)         {             OpenFileDialog ofd = new OpenFileDialog ();   ...

ImageEditor in C# Visual Studio 2017

ImageEditor in C # Start a new project in Visual Studio > choose Visual C# then click on Windows Desktop then select WindowsForm App(.Net Framework)  and name it as ImageEditor then press ok. First, go to the Form1 properties  S ection and set the Size as following. Form1-size=952, 607 Now Add 3 panels from the toolbox and set the properties as follows. After setting the properties it will look as follows. Now Add 2-picture box from the toolbox and go to the properties and set the properties as follows. Now add 15-Button from the toolbox. Now add 3-textbox from the toolbox. Now add 3-trackbar and 5-label from the toolbox. Now go to the properties section and check all the Components are named correctly or not. Once  all the Components are named correctly then  move to the coding part. (Now the Coding Part) Now go to the Form1 code view and  ...

Microsoft tools Download

1-Visual Studio Code Visual Studio Code is a  fast and free  source code lite editor that can be used with the various programming languages. Download 2-Visual Studio 2019 Community Download 3-Microsoft Visual Studio 2010 Microsoft Visual Studio 2010 Development environment specialized in Windows systems. Download 4-VISUAL STUDIO 2013 EXPRESS FOR WEB Visual Studio Express 2013 for Web provides the core tools for creating compelling, innovative web applications and services. This includes the ASP.NET editors for HTML5, CSS3, JavaScript, etc. Download 5-Piskel Download 6-Pichon Download 7-Microsoft XNA Framework Redistributable 4.0 Description The XNA Framework has all the necessary runtime components to execute a game, which was developed using Microsoft XNA Game Studio 4.0. Version 4.0 of the framework contains improved functionality and new features.