We will now discuss several C# programming language methods and implement them in the Ubuntu 20.04 environment.
Method 01: Using the Stream Writer and Stream Reader Classes in Ubuntu 20.04 to Create and Read Text Files
In this method, we will be using the Stream Writer and Stream Reader class to create a text file while adding some text to them. The Stream writer class is a part of the System.IO namespace and is used for input functions in a text file, while the Stream Reader class, which is also present in the same namespace, is used for reading the text file in the C# programming language. Both these classes will help us in achieving our goal in this method.
The above C# program starts with the initialization of a string variable named “file Name.” We gave this variable the text file name that we will be creating and reading on our device. Then we will be initiating the creation of the object of the Stream Writer class, which will have the File.CreateText Function, and this function will have the string variable “file Name” which has the name of the text file. Within the parameters of the stream writer class’s object, we have used the object to call the Write Line method to add some lines to the new text file created.
After this, we will be initiating an object for the Stream Reader class, which will have the Open Text function with the File name string variable; this function will give us access to the text file. Now we will initiate a string variable and put it in a while loop which will have the stream reader object assigning all the text of the file to the string variable, and it will all be printed using the Console Write Line function.
After compiling and executing the previous code, we will get the above output. In this snippet, we can see the output of the while loop in the Stream Reader function, which reads the text file and prints its content. We will look at the text file that we created and verify that the text was added successfully.
Method 02: Using File Stream and Stream Reader Class to Create and Read a Text File in Ubuntu 20.04
In this technique, we will create a text file and add some text to it using the File Stream and Stream Reader classes. For text file input methods, the File Stream class from the System.IO namespace will be utilized, while the Stream Reader class from the same namespace will be used to read the text file in this method.
The C# code above starts with initializing a string variable named “f.” This variable is named after the text file we will be creating. Then we will start the Stream Writer class object, which will have a “File.CreateText Function” with a string variable containing the file name. We have used the object to use the Write Line function to add some lines to the new text file generated using the stream writer class’s object’s arguments.
After that, we will create an object for the Stream Reader class using the “Open Text” method and the File name string variable, which will allow us to read the text file. Now we will create a string variable and place it in a while loop, with the stream reader object assigning all of the file’s text to the string variable and printing it all using the “Console Write Line” method.
We will obtain the above output after compiling and running the prior code. The result of the while loop of the Stream Reader function, which reads the text file and displays its content, can be seen in this snippet. We will now examine the text file we prepared.
The text file has the line that we added to it using the Stream Writer Class object, as we can see in the following example.
Method 03: Using File Security and File Stream Class to Create a Text File With Secure Access in Ubuntu 20.04
In this method, we will be using the File Security class to restrict the access of a text file, and only specified users will be allowed to open and edit the file. The File Security class and the File Stream Class are both found in the System.IO namespace and will be utilized to create a text file on our system in this function.
In this C# program, we will initialize a string variable with the name of the text file stored in it. Then we will create an object of the File Security class and use it for the “Add Access Rule function”, which is the further inheritance for the File System Access Rule; this will allocate the new text file its access rights. After this, we will create an object of the File Stream class to create a text file, and in the parameters, we will pass the object of the File Security class along with file size and other details. Then with the help of the Byte array, we will add some text to the file as well.
In this output, we can see that the text file was created successfully as the file contents were displayed in the terminal.
Method 04: Using File.Write All Text to Create and Add Text to a Text file in Ubuntu 20.04
In this method, we will be using the functions of the System.Text namespace to create a text file. This namespace in the C# Programming language has all the functionalities related to textual operations. In this method, we will use the Write All Text function of the System.Text namespace to create and add text to a new text file.
In this C# program, we will initialize two string variables, one with the file name and the other with the text that we want to add to the file. Then we will call the Write All Text function with both these string variables as the parameter.
After executing the above program, we will get the following output:
The terminal and the text file are evidence that the Write All Text function successfully created a text file with some text in it.
Conclusion
In this article, we discussed several C# programming language methods to create a text file on your device. The use of special classes built for editing and creating text files present in the System.IO namespace, which are some special operations(input and output) libraries related to text files in the C# programming languages, were also discussed in this article. We focused on the function of stream classes to create a file which is the Create() method and was common in several classes. We implemented this method and other text file creating methods in the Ubuntu 20.04 environment to better understand how these different classes and functions work together in the C# programming language.