‫Welcome back.
‫In this video, you're going to learn how to read from a text file.
‫And therefore, I created a small text file and I have a folder here called Assets.
‫And in there I just created this text file, dot text.
‫And if I open that up, you can see it's just as hello world, then it's here.
‫So that's it, nothing else.
‫Very simple text file.
‫And if you want to go along with this video and build it yourself, you can also create a little text
‫file yourself just like that.
‫All right.
‫So in order to read from a file, you need to go ahead and create a string into which you want to save
‫that file or save the text of that file.
‫So I'm just going to say string text is going to be and here we use system IO standing for input output
‫and there is a class called file and that class generally has multiple methods which take care of reading,
‫opening text and so forth.
‫So you can see there are plenty of methods and the one that we want to use is this read all text method
‫and we'll read the whole text of a specific file.
‫So here you need to specify where your file is or what the complete directory of that file is, and
‫you need to use this add sign before that.
‫So in here I'm just going to copy my location or the directory and then here I simply copy that part
‫here.
‫So I market and then copy it and then I only need to add the text file.
‫So backslash text file, dot pt text.
‫So it's important that is exactly the right directory, otherwise it won't work.
‫So we have this file here with the whole directory.
‫So see w and in here I'm going to say something like text file contains following text and it's going
‫to be zero comma text.
‫So that's pretty much it.
‫That's what my console will print out and I'm going to use console the right key so that we can see
‫what's going on in the file.
‫All right.
‫So let's start that.
‫Let's check it out.
‫And the you are text file contains following text.
‫Hello world then is here.
‫So we have simply read the text and now we could use that text to do some execution or stuff like that.
‫You can, for example, if you create a little game and you want to store the high score, then you
‫could actually store that just within a file where you simply add the text name and maybe even the username
‫that has one or has received that high score or made the high score.
‫All right.
‫So that's version one or example one
‫reading text.
‫Now, there is another way to do that, and it's the following where you read every single line.
‫So I'm going to use a string array here, which I'm going to call lines and that one also takes system
‫that I IO file.
‫And now in the file class it has this read all lines.
‫As you can see, it can go ahead and read lines, read all texts, read all lines and read all bytes.
‫But the one that I want to use is this read lines or actually all lines.
‫And which lines should read?
‫Well, all lines from that specific file, and it's going to be the same file.
‫So I'm going to use it like that here as well.
‫All right.
‫So now that we have the location, let's use that for our console.
‫So here some colon is missing.
‫Now we can go ahead and so write something onto the console like that.
‫C W Contents of text file, that's text.
‫R And now I'm going to use it for each.
‫And in this for each loop I'm going to say give me line in lines and do something with it.
‫So I'm going to use the console right line here again and I'm going to say something like backslash
‫t, which is going to create a new tab plus the line.
‫So that will do is it will write a text and it will add a tab and then the text will come out.
‫So it's a little differently readable.
‫So you could say it's better to read it like that.
‫But yeah, it's depends on what you prefer.
‫So there you are.
‫This is the first text that we had.
‫So Hello World and Dennis here.
‫So it just printed it out the way that it was in the file, even the next line and here contents of
‫text, file, dot, text while we get each single line and we print it out as an individual line with
‫right line here and we add this whole tab here which is this backslash.
‫T So these are, these are the two ways of reading from a text file.