c sharp

C# StreamWriter

In this article, we will be discussing the Streamwriter class in the C# programming language. The Streamwriter class is usually used for handling text data in files; it helps in assisting when we alter or add text in a file with its different methods and constructors. The StreamWriter class is defined in the System.IO namespace, and it is an inheritance of the TextWriter class in the C# Programming Language.

Using StreamWriter class in C# to write text in a file in Ubuntu 20.04:

Now we will utilize the Streamwriter class’s methods and constructors to write text in a “.txt” format file. The “C# file” will be compiled, and the text file will be edited using the command line interface. We will open the desktop directory to create the C# file and the text file on our desktop for easy access; for this; we will use the “ cd Desktop “ command. After that, we’ll create a “.cs” file in which we’ll write a ” C# ” program to write text to a text file.

In the above code, we have used the streamwriter class and made an object of the class to call its functions that will assist us in writing text into a file that we created on our desktop. The class object is then called to get us access to the “Write()” method, which is responsible for writing text into a text file, and the other method, which is the “WriteLine()” method which is responsible for writing a line into a text file.

The terminal output will display that the data is saved successfully. We will also open the “.txt” file that we made by finding it on our desktop and double-checking the text.

As we can see below, the text file has some text written in it that we wrote in our code earlier, which further confirms that our program executed the operation successfully.

Inserting a line in a text file using streamwriter class in C# in Ubuntu 20.04:

As we already wrote a word successfully in a text file, we will move on to writing a whole in a text file. For this operation, we will use the “WriteLine()” function, which enables us to write text in lines in text files. We will write a C# program in which we will create a file and then use the streamwriter class to write a line in a text file.

We utilized the streamwriter class in the following code and generated an object of the class to use its procedure, the “WriteLine()” method, which would let us write a line into a file that we created on our desktop.

The data is stored successfully as the command line terminal suggests. We’ll also validate the text in the “.txt” file we produced by finding it on our desktop and viewing it.

The “.txt ” file shows that a single line has been added to the text file, and this verifies that our program ran successfully.

Inserting multiple lines in word in a text file using streamwriter class in C# in Ubuntu 20.04:

Now our focus would be on adding multiple lines to a text file using the Streamwriter class in C#. We will initialize a text file on our desktop and then make an object of the Streamwriter class by which we will access the function of the Streamwriter class that is “WriteLine()” for inserting text lines to the “.txt ” file.

In the following code, we used the streamwriter class and built an instance of the class to use its operations, which allowed us to add multiple lines to a file we made on our desktop. We used the “WriteLine()” method of the StreamWriter class to write multiple lines in our text file.

The terminal output will display a success message of the saving of the data in our text file. We will also verify the saving of the data by opening and analyzing the text file that is present on our desktop.

As the file shows that multiple lines have been updated in the text file, which is a confirmation of adding multiple lines of data in a text file. The use of the “WriteLine()” is efficient for inserting and saving lines into a text file, as our program suggests.

Saving a variable in a text file by using streamwriter class in C# programming language in Ubuntu 20.04:

In this example, we will save a variable in a text file and perform a mathematical operation before saving it in the text file. We’ll start by creating a text file on our desktop and then initializing three integers and assigning two of them some value, while the third integer will be the product of these two integers. After assigning values and functions, we will use the streamwriter class to save the third integer variable in the text file.

In the above code, we have initialized 3 integer variables in our code and assigned two of them a value, while the third integer is stated to be the product of the first two integers. After that, we used the “Write()” method to insert and save these variables into the text file.

The output message shown in the terminal provides evidence that the data in our text file is saved which can also be verified by viewing the text file from our desktop.

The text file shows the line in which the variable is saved. The resultant of the sum of the two integers, which is “8,” is displayed in the last part of the line.

Using StreamWriter class with FileStream class to create and insert data into a text file in C# programming language in the Ubuntu 20.04 environment:

For this example, we will be using the Streamwriter class with the FileStream class to create and save data in a text file coherently. The filestream class is used for all functions related to files in the C# programming languages like open, close, read and write. This class is defined in the System.IO namespace in C# programming. The filestream class will create a file on our desktop by using its “Create” function, while the StreamWriter class will save some data in that file.

In the above code, we have used both the classes, which are FileStream and StreamWriter, to create a file on our desktop. By creating instances of the classes, we were able to use their “Create” and “Close” functions.

The output of the terminal reflects that the text file is created, and some data is saved in that file, which can also be accessed from the desktop for verification as shown below:

Conclusion:

In this article, we discussed the StreamWriter class in the C# programming language. This class is usually called upon when we are required to add text to a “ .txt ” file on the run time. The StreamWriter class has several functions that assist in inserting and saving data; these functions are accessed by instances. We implemented multiple examples of these instances and different scenarios in the Ubuntu 20.04 environment to have a better understanding of the StreamWriter class.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.