In this lesson, I will speak a bit about reading and writing files in C # because you will surely write in the future some program in which you will have to save some data, eg a game in which you will have to save the current state of the game, to the point!
In C #, we use streams to read and write data, whenever we save any data we use streams.
If we want to save or read any data, we use the Stream object.
With streams you can do min:
– write data to the stream
Using his Write() method, you can:
– Read data from the stream
to download data from a file we use the Read() method
– Change the position inside the stream
Normal streams provide the Seek() method that allows you to write or read files in a specific location.
Let’s create a directory in which we will save this data and, for example, save some data to a file using streams in the new console application. Let’s write something like that in the Main function and do not run it yet.
static void Main(string args)
FileStream saveFile = new FileStream("C:\\Users\\" + Environment.UserName + "\\Desktop\\lessonaboutfiles.txt", FileMode.Open, FileAccess.Write);
StreamWriter readFile = new StreamWriter(saveFile);
readFile.WriteLine("Let's save some text as part of the lesson");
However, mistakes pop up. So that we can run this program, we have to write the line in the namespaces:
Now we can run this program and when you see the desktop you will see the file lessonaboutfiles.txt there.
What is this program? First, we create the right path for this file (we want to create it on the desktop) and we have to share it for writing with the line FileAccess.Write.
Next, we create the StreamWriter object, that is, the object used to save the data and write data to the file using the WriteLine method. At the end, you always have to close the stream and path to the file using the Close() method.
However, you noticed that the file and the StreamWriter object are just for writing as we will try to read something, they will crash the error, so how do you make the file to read?
Let’s save something like this in Main’s function:
static void Main(string args)
FileStream ReadFile = new FileStream("C:\\Users\\" + Environment.UserName + "\\Desktop\\lessonaboutfiles.txt", FileMode.Open, FileAccess.Read);
FileStream SaveFile = new FileStream("C:\\Users\\" + Environment.UserName + "\\Desktop\\lessonaboutfilesread.txt", FileMode.Create, FileAccess.ReadWrite);
StreamWriter streamfilesave = new StreamWriter(SaveFile);
StreamReader streamfileread = new StreamReader(ReadFile);
string read = streamfileread.ReadLine();
streamfilesave.WriteLine("Read this data -> " + read);
What has changed here? The file that was previously set to write data is now set to read and we have created a new file that is now set to read and write.
Next, we create a read and write object, in the read object we pass a file in which we already have something and in the object to be read we pass the newly created file.
Next, we create a while loop in which we enter a property that checks if there is any data in the file, in the body of this loop we create a variable to which we write the read lines from the file and save the data to the newly created file.
At the end, of course, we close the streams of objects and files.
After starting the program, you should see something like this in the newly created file:
“Read this data-> Save some text as part of the lesson”
Of course, there are easier ways to save and read data as I showed these ways to show how it looks. Therefore, in the next lesson, it will be article about the easier way to read and write data, ie serialization.
This content also you can find on my steemit blog: https://steemit.com/programming/@slawas/c-language-file-operations
See you later!