Intro

For writing and reading textfiles we use two classes, provided by the framework. Those classes are called: StreamReader and StreamWriter

Stream

By using data we distinguish between storing and processing

Processing Data

While storing data is pretty self-explanatory we have two types of processes.

Batch Processing and Stream Processing

Batch processing means to process all the data at once, while stream processing processes the data continuous, in real-time.

Handling Stream Data

Data Streams work in many different ways.

Typpically when we need data from a data storage, we have to send a request and recieve a respnonse. This is called polling.

Refering to C#

When reading or writing data we have to stream data.

Rundown of streaming data in C#
  1. Opening a stream. When calling the constructor of a "stream class" we define the pathfile.
  2. Processing the data by using the methods the "stream class" offers us to use - it is key to understanding StreamReader and StreamWriter methods to work with text files.
  3. Closing the stream to prevent problems.

StreamWriter

The StreamWriter class, as the StreamReader class, are inside the System.IO namespace (Input/Ouput).

So to have less typing we include the namespace:

using System.IO;
Opening the stream
StreamWriter stream = new StreamWriter("../../textfile.txt");
Processing Data

For appending data to the text file we can use the Write() and Writeline() method. The behave exatly like the methods Console.WriteLine() and Console.Write(), except the write not to the console window, but to the stream.

"../../textfile.txt" we go two folders up, since the StreamWriter is executed in build (or debug) direcotry and not in the same folder as the code file.

StreamWriter stream = new StreamWriter("../../textfile.txt");
stream.WriteLine("Hello");
stream.Write("World");
stream.Close();

Produces this textfile:

Hello
World

StreamReader