‫Welcome back.
‫Now that you know how to read from a file, let's go ahead and write a file as well.
‫So what I'm going to do is I'm going to comment all the code out until there actually.
‫So here like that.
‫And on top of that, I'm going to go ahead and add the writing stuff.
‫So I'm going to start with a string array, which I'm going to call lines because there are two ways
‫of writing into your file as well.
‫You can write by line or you can simply write a string into a file.
‫And we're going to start with the array type or with the different lines.
‫So I create multiple different lines and I'm going to start with the first line, then I'm going to
‫create a second line.
‫And finally the third line, just like that.
‫And now I can write that onto a file and that will be done with the file class again, and you can either
‫import it at the top.
‫So using actually we can do it this way.
‫We haven't done it last time system, dot io dot file or actually like that file or maybe even just
‫the IO, let's just import the IO and now we can use file the file class and we can write all lines.
‫And here we need two different arguments.
‫One is the path and one is the contents, which is you can see is an array of strings.
‫So the path I'm going to use the same path as we did last time.
‫So I'm just going to copy that.
‫And then I need the lines in our case.
‫It's just those lines.
‫An array of strings.
‫Right.
‫So that's how I can write into a file or I can create a file, and I'm going to write into a text file,
‫too.
‫So let's start that and also let's check out the location here.
‫So this is our folder and as you can see, it created a file too.
‫So let's check it out.
‫And there we are.
‫First line, second line, third line.
‫All right.
‫So now you know how to write and how to read from a file.
‫So now go ahead and write the high score of three users into an array.
‫Go ahead and please try that.
‫All right.
‫So the first one, the first high score, let's say, was 250.
‫Then the second high score will be 242.
‫And then we have 240.
‫So these are high scores and I'm going to store that in high scores.
‫Dot text.
‫Now let's create that.
‫So you see, this is the console.
‫Nothing too fancy going on in the console.
‫And now the assets folder.
‫And there you see high scores were created.
‫So now you could go ahead and read from that file and well read by line and then update your game where
‫you display the high scores.
‫All right.
‫So now there is another way, because this way we are just writing a whole well set of lines and now
‫we just write a text.
‫So let's go ahead and create a text string, which is going to be input and that will be console that
‫read line.
‫So we just more actually read as fine as well.
‫So we're just going to read whatever the user has entered and now I want to write that into my file.
‫So file dot and as you can see, we have those right methods here as well.
‫So we use the read methods, read all lines and read text and now we're going to use right all text.
‫So I'm going to write all text into a path and I'm just going to copy the path here like that.
‫And by the way, you can use something like that plus and here.
‫Let's create one like this.
‫So this one will be file name.
‫So we're going to add the file name.
‫Plus txt.
‫So dot txt.
‫And then we need the contents.
‫So the contents will be input like that.
‫So let's inform the user as well that you can enter a text.
‫So I'm going to say c w please give the file a name.
‫And now the next one will be c w.
‫Please enter the text for the file.
‫And that will be the input of the user.
‫All right.
‫So now let's write that into the console.
‫Please give a file name I'm going to call it test file is enter the text for the file.
‫Hi there.
‫This is a test.
‫And we enter and we enter again.
‫There we are.
‫So now let's check it out.
‫Let's see if the test file was created.
‫There we are.
‫Test file is created.
‫And you see.
‫Hi there.
‫This is a test.
‫So you can see how you can create test text files quite easily, even using the user or the file name
‫yourself.
‫So we created an own file here.
‫All righty.
‫So these are two ways of writing into a file or writing into a text file.
‫So method one and that one is method two.
‫And now there is actually a method three.
‫So method three.
‫And that is to use a stream writer.
‫So using system dot IO, which we already had, so we can go ahead and create the stream writer functionality.
‫So Stream Writer File is going to be a new stream writer.
‫And in here I need to enter the location.
‫So I'm going to use that one here, the path.
‫So I'm going to enter the path and this time I'm going to call it my text, something like that.
‫Now what we need to do is to actually write something on it.
‫And I'm going to read through or go through every single line that we have here with the for each group
‫and going to write it into my console, but only if it contains the word third.
‫All right.
‫So let's go ahead for each string line in lines.
‫And here should be line, of course.
‫So if line dot contains.
‫The word third.
‫So only if the line contains the word third.
‫Then I want to go ahead and write the line into the file.
‫All right.
‫So let's check it out.
‫This one was third, right?
‫So I'm going to add third in here, third, 242nd like that.
‫And then that one was first.
‫So I only want to write into the file if there was a word third in that specific line that we're looking
‫at.
‫And now the cool thing with the Stream writer is that it opens and it closes as required.
‫So we don't need to do all of that manually.
‫So now let's run it again.
‫And actually we need to give a new file, a name, new file.
‫Well, let's do it that way.
‫Let's get rid of our second method for now, because otherwise it will force us to put something in.
‫And here we are now we can actually check our assets and we see my text was created.
‫So that's the third file on this file here.
‫My text, I'll text, let's open it up and we see only third was given here, so only third was printed
‫into that file.
‫So you can do something like using a lines and then checking if they have a specific content.
‫So this is one of the methods to check for content.
‫And this is here line, which is the particular line that we're currently looking at in all of our lines.
‫And then we check if it contains the term third.
‫And if it does well, then please go ahead and write it and otherwise don't write it.
‫So we could create another text called Well actually try yourself create another text which is called
‫my text two and it should only write the lines that contain the two, which contain a two in their name.
‫All right.
‫I hope you tried it.
‫So this one and it's going to be my text, too.
‫And line contains a two.
‫And now let's write or let's run the code again and I'm going to close the straight away so you can
‫go to ah my text two, which is the new file and as you can see all the lines were written.
‫And why is that?
‫Well because all of them contain a two.
‫So if you would also want to only have the ones with a four, then you could only get second and third
‫into a file or written to a file.
‫So what if you want to add something to that file?
‫So add text to the file.
‫Well, then you can simply go ahead and go as follows.
‫So we're going to create a new line of code here called Using Stream Writer,
‫and it's going to be a file that I'm creating.
‫And that file should be a new stream writer.
‫And then here I give the path again like we had before.
‫So the argument is going to be a path and the second one is going to be a true so boolean.
‫What that will allow us to do is to write onto our file.
‫So file, dot, right line and I'm going to say something like additional line like that.
‫So now let's run it again.
‫And let's check out our file.
‫My text, too.
‫And we see additional line was printed to it so that you could go ahead and write all the different
‫scores that players have created, have made or had.
‫And you can file, write them all into a file or whatever you want.
‫You can write it into your file now, and that's pretty cool.
‫All right.
‫So now you know how to read and write files, which are text files.