C# Streams :

Streams :

Streams are an essential part of any input-output library. You can use streams when your program needs to "read" or "write" data to an external data source such as files, other PCs, servers etc. It is important to say that the term input is associated with reading data, whereas the term output is associated with writing data.

Binary and Text Streams :

As we mentioned earlier, we can divide the streams into two large groups according to the type of data that we deal with – binary streams and text streams.

Binary Streams :

Binary streams, as their name suggests, work with binary (raw) data. You probably guess that that makes them universal and they can be used to read information from all sorts of files (images, music and multimedia files, text files etc.). We will take a brief look over them, because we will currently focus on working with text files.

Text Streams :

Text streams are very similar to binary, but only work with text data or rather a sequence of characters (char) and strings (string). Text streams are ideal for working with text files. On the other hand, this makes them unusable when working with any binaries.


Writing to a Text File ( Input ) :

Text files are very convenient for storing various types of information. For example, we can record the results of a program. We can use text files to make something like a journal (log) for the program. Here we use StreamWriter for input/write data.

Example :

  1. /* Description: input/store data to a text file */
  2. using System;
  3. using System.IO;
  4. namespace DemoProgramming
  5. {
  6. class Program
  7. {
  8. static void Main()
  9. {
  10. StreamWriter writer = new StreamWriter("numbers.txt");
  11. using (writer)
  12. {
  13. for (int i = 1; i <= 20; i++)
  14. {
  15. writer.WriteLine("No: "+i);
  16. }
  17. }
  18. Console.WriteLine("Data Store Successfully!");
  19. Console.ReadKey();
  20. }
  21. }
  22. }

Output :

C Sharp Language input/store data to a text file

Reading to a Text File ( Output ) :

Now, we have learned how to create StreamReader. We can go further by trying to do something more complicated: to read the entire text file line by line and print the read text on to the console. Our advice is to create the text file in the Debug folder of the project (.\bin\Debug), so that it will be in the same directory in which your compiled application will be and you will not have to set the full path to it when opening the file.

Example :

  1. /* Description: output/read data to a text file */
  2. using System;
  3. using System.IO;
  4. namespace DemoProgramming
  5. {
  6. class Program
  7. {
  8. static void Main()
  9. {
  10. StreamReader reader = new StreamReader("numbers.txt");
  11. int lineNumber = 0;
  12. string line = reader.ReadLine();
  13. while (line != null)
  14. {
  15. lineNumber++;
  16. Console.WriteLine("Line {0}: - {1}", lineNumber, line);
  17. line = reader.ReadLine();
  18. }
  19. reader.Close();
  20. Console.WriteLine("Data Read Successfully!");
  21. Console.ReadKey();
  22. }
  23. }
  24. }

Output :

C Sharp Language output/read data to a text file

Computer Science Engineering

Special Notes

It's a special area where you can find special questions and answers for CSE students or IT professionals. Also, In this section, we try to explain a topic in a very deep way.

CSE Notes