C# language: File operations

in programming •  7 years ago 

CC.png

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");
 
    readFile.Close();
    saveFile.Close();
}

However, mistakes pop up. So that we can run this program, we have to write the line in the namespaces:

USING SYSTEM.IO;

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);
 
     while (!streamfileread.EndOfStream)
     {
         string read = streamfileread.ReadLine();
         streamfilesave.WriteLine("Read this data -> " + read);
     }
 
     streamfilesave.Close();
     streamfileread.Close();
     SaveFile.Close();
     ReadFile.Close();
}

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 blog http://devman.pl/csharplan/c-language-file-operations/

If you recognise it as useful, share it with others so that others can also use it.

Leave upvote and follow and wait for next articles :) .

See you later!

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!