1 00:00:00,450 --> 00:00:01,440 ‫Welcome back. 2 00:00:01,470 --> 00:00:06,240 ‫Now that you know how to read from a file, let's go ahead and write a file as well. 3 00:00:06,240 --> 00:00:15,300 ‫So what I'm going to do is I'm going to comment all the code out until there actually. 4 00:00:15,420 --> 00:00:18,000 ‫So here like that. 5 00:00:18,150 --> 00:00:23,250 ‫And on top of that, I'm going to go ahead and add the writing stuff. 6 00:00:23,250 --> 00:00:30,270 ‫So I'm going to start with a string array, which I'm going to call lines because there are two ways 7 00:00:30,270 --> 00:00:31,830 ‫of writing into your file as well. 8 00:00:31,830 --> 00:00:36,960 ‫You can write by line or you can simply write a string into a file. 9 00:00:36,960 --> 00:00:41,400 ‫And we're going to start with the array type or with the different lines. 10 00:00:41,400 --> 00:00:45,810 ‫So I create multiple different lines and I'm going to start with the first line, then I'm going to 11 00:00:45,810 --> 00:00:48,030 ‫create a second line. 12 00:00:48,330 --> 00:00:55,470 ‫And finally the third line, just like that. 13 00:00:55,470 --> 00:01:04,110 ‫And now I can write that onto a file and that will be done with the file class again, and you can either 14 00:01:04,110 --> 00:01:05,610 ‫import it at the top. 15 00:01:05,610 --> 00:01:08,370 ‫So using actually we can do it this way. 16 00:01:08,370 --> 00:01:18,330 ‫We haven't done it last time system, dot io dot file or actually like that file or maybe even just 17 00:01:18,330 --> 00:01:27,960 ‫the IO, let's just import the IO and now we can use file the file class and we can write all lines. 18 00:01:28,440 --> 00:01:31,950 ‫And here we need two different arguments. 19 00:01:31,950 --> 00:01:37,560 ‫One is the path and one is the contents, which is you can see is an array of strings. 20 00:01:37,560 --> 00:01:41,580 ‫So the path I'm going to use the same path as we did last time. 21 00:01:41,580 --> 00:01:43,110 ‫So I'm just going to copy that. 22 00:01:43,920 --> 00:01:50,400 ‫And then I need the lines in our case. 23 00:01:51,350 --> 00:01:52,460 ‫It's just those lines. 24 00:01:52,640 --> 00:01:53,480 ‫An array of strings. 25 00:01:53,480 --> 00:01:53,960 ‫Right. 26 00:01:54,170 --> 00:02:03,470 ‫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, 27 00:02:03,470 --> 00:02:03,980 ‫too. 28 00:02:04,220 --> 00:02:09,790 ‫So let's start that and also let's check out the location here. 29 00:02:09,800 --> 00:02:14,390 ‫So this is our folder and as you can see, it created a file too. 30 00:02:14,420 --> 00:02:15,670 ‫So let's check it out. 31 00:02:15,680 --> 00:02:16,590 ‫And there we are. 32 00:02:16,610 --> 00:02:18,890 ‫First line, second line, third line. 33 00:02:19,670 --> 00:02:20,300 ‫All right. 34 00:02:20,960 --> 00:02:24,990 ‫So now you know how to write and how to read from a file. 35 00:02:25,010 --> 00:02:31,010 ‫So now go ahead and write the high score of three users into an array. 36 00:02:31,040 --> 00:02:32,840 ‫Go ahead and please try that. 37 00:02:35,460 --> 00:02:36,120 ‫All right. 38 00:02:36,570 --> 00:02:43,830 ‫So the first one, the first high score, let's say, was 250. 39 00:02:45,420 --> 00:02:52,440 ‫Then the second high score will be 242. 40 00:02:52,440 --> 00:02:55,740 ‫And then we have 240. 41 00:02:56,520 --> 00:03:01,980 ‫So these are high scores and I'm going to store that in high scores. 42 00:03:02,630 --> 00:03:03,690 ‫Dot text. 43 00:03:05,130 --> 00:03:06,330 ‫Now let's create that. 44 00:03:08,070 --> 00:03:09,610 ‫So you see, this is the console. 45 00:03:09,630 --> 00:03:11,990 ‫Nothing too fancy going on in the console. 46 00:03:12,000 --> 00:03:13,890 ‫And now the assets folder. 47 00:03:13,890 --> 00:03:16,190 ‫And there you see high scores were created. 48 00:03:16,200 --> 00:03:22,920 ‫So now you could go ahead and read from that file and well read by line and then update your game where 49 00:03:22,920 --> 00:03:24,630 ‫you display the high scores. 50 00:03:25,500 --> 00:03:26,100 ‫All right. 51 00:03:26,100 --> 00:03:33,600 ‫So now there is another way, because this way we are just writing a whole well set of lines and now 52 00:03:33,600 --> 00:03:35,170 ‫we just write a text. 53 00:03:35,190 --> 00:03:46,330 ‫So let's go ahead and create a text string, which is going to be input and that will be console that 54 00:03:46,350 --> 00:03:48,600 ‫read line. 55 00:03:50,840 --> 00:03:53,960 ‫So we just more actually read as fine as well. 56 00:03:53,960 --> 00:03:59,660 ‫So we're just going to read whatever the user has entered and now I want to write that into my file. 57 00:03:59,660 --> 00:04:05,390 ‫So file dot and as you can see, we have those right methods here as well. 58 00:04:05,390 --> 00:04:11,060 ‫So we use the read methods, read all lines and read text and now we're going to use right all text. 59 00:04:11,060 --> 00:04:19,370 ‫So I'm going to write all text into a path and I'm just going to copy the path here like that. 60 00:04:19,700 --> 00:04:31,190 ‫And by the way, you can use something like that plus and here. 61 00:04:33,340 --> 00:04:35,400 ‫Let's create one like this. 62 00:04:35,410 --> 00:04:38,530 ‫So this one will be file name. 63 00:04:41,420 --> 00:04:43,520 ‫So we're going to add the file name. 64 00:04:46,180 --> 00:04:49,840 ‫Plus txt. 65 00:04:49,870 --> 00:04:51,340 ‫So dot txt. 66 00:04:52,660 --> 00:04:55,240 ‫And then we need the contents. 67 00:04:55,240 --> 00:04:59,680 ‫So the contents will be input like that. 68 00:05:00,130 --> 00:05:05,050 ‫So let's inform the user as well that you can enter a text. 69 00:05:05,050 --> 00:05:13,060 ‫So I'm going to say c w please give the file a name. 70 00:05:14,710 --> 00:05:19,030 ‫And now the next one will be c w. 71 00:05:19,960 --> 00:05:24,310 ‫Please enter the text for the file. 72 00:05:27,050 --> 00:05:29,000 ‫And that will be the input of the user. 73 00:05:29,030 --> 00:05:29,450 ‫All right. 74 00:05:29,450 --> 00:05:32,810 ‫So now let's write that into the console. 75 00:05:34,900 --> 00:05:41,590 ‫Please give a file name I'm going to call it test file is enter the text for the file. 76 00:05:41,860 --> 00:05:42,860 ‫Hi there. 77 00:05:42,880 --> 00:05:45,430 ‫This is a test. 78 00:05:47,020 --> 00:05:49,720 ‫And we enter and we enter again. 79 00:05:50,080 --> 00:05:50,670 ‫There we are. 80 00:05:50,680 --> 00:05:51,910 ‫So now let's check it out. 81 00:05:52,360 --> 00:05:54,100 ‫Let's see if the test file was created. 82 00:05:54,100 --> 00:05:54,630 ‫There we are. 83 00:05:54,640 --> 00:05:55,690 ‫Test file is created. 84 00:05:55,690 --> 00:05:56,440 ‫And you see. 85 00:05:56,440 --> 00:05:56,860 ‫Hi there. 86 00:05:56,860 --> 00:05:57,760 ‫This is a test. 87 00:05:57,760 --> 00:06:06,040 ‫So you can see how you can create test text files quite easily, even using the user or the file name 88 00:06:06,040 --> 00:06:06,610 ‫yourself. 89 00:06:06,610 --> 00:06:08,680 ‫So we created an own file here. 90 00:06:09,490 --> 00:06:10,230 ‫All righty. 91 00:06:10,240 --> 00:06:17,680 ‫So these are two ways of writing into a file or writing into a text file. 92 00:06:17,890 --> 00:06:24,280 ‫So method one and that one is method two. 93 00:06:24,520 --> 00:06:26,320 ‫And now there is actually a method three. 94 00:06:26,320 --> 00:06:29,830 ‫So method three. 95 00:06:30,610 --> 00:06:33,760 ‫And that is to use a stream writer. 96 00:06:33,760 --> 00:06:46,240 ‫So using system dot IO, which we already had, so we can go ahead and create the stream writer functionality. 97 00:06:46,240 --> 00:06:54,250 ‫So Stream Writer File is going to be a new stream writer. 98 00:06:55,750 --> 00:07:00,040 ‫And in here I need to enter the location. 99 00:07:00,040 --> 00:07:03,970 ‫So I'm going to use that one here, the path. 100 00:07:05,140 --> 00:07:13,330 ‫So I'm going to enter the path and this time I'm going to call it my text, something like that. 101 00:07:14,550 --> 00:07:19,960 ‫Now what we need to do is to actually write something on it. 102 00:07:19,960 --> 00:07:25,210 ‫And I'm going to read through or go through every single line that we have here with the for each group 103 00:07:25,210 --> 00:07:32,140 ‫and going to write it into my console, but only if it contains the word third. 104 00:07:32,620 --> 00:07:33,040 ‫All right. 105 00:07:33,040 --> 00:07:37,540 ‫So let's go ahead for each string line in lines. 106 00:07:39,370 --> 00:07:41,020 ‫And here should be line, of course. 107 00:07:41,020 --> 00:07:45,580 ‫So if line dot contains. 108 00:07:47,040 --> 00:07:48,840 ‫The word third. 109 00:07:50,250 --> 00:07:52,970 ‫So only if the line contains the word third. 110 00:07:52,980 --> 00:07:59,160 ‫Then I want to go ahead and write the line into the file. 111 00:08:01,150 --> 00:08:01,430 ‫All right. 112 00:08:01,430 --> 00:08:02,470 ‫So let's check it out. 113 00:08:02,480 --> 00:08:03,740 ‫This one was third, right? 114 00:08:03,740 --> 00:08:10,820 ‫So I'm going to add third in here, third, 242nd like that. 115 00:08:11,090 --> 00:08:13,940 ‫And then that one was first. 116 00:08:13,940 --> 00:08:20,300 ‫So I only want to write into the file if there was a word third in that specific line that we're looking 117 00:08:20,300 --> 00:08:20,750 ‫at. 118 00:08:20,750 --> 00:08:26,000 ‫And now the cool thing with the Stream writer is that it opens and it closes as required. 119 00:08:26,000 --> 00:08:29,300 ‫So we don't need to do all of that manually. 120 00:08:30,560 --> 00:08:32,450 ‫So now let's run it again. 121 00:08:32,930 --> 00:08:37,430 ‫And actually we need to give a new file, a name, new file. 122 00:08:37,430 --> 00:08:38,990 ‫Well, let's do it that way. 123 00:08:39,000 --> 00:08:53,600 ‫Let's get rid of our second method for now, because otherwise it will force us to put something in. 124 00:08:53,600 --> 00:08:59,990 ‫And here we are now we can actually check our assets and we see my text was created. 125 00:09:00,350 --> 00:09:02,690 ‫So that's the third file on this file here. 126 00:09:02,690 --> 00:09:09,680 ‫My text, I'll text, let's open it up and we see only third was given here, so only third was printed 127 00:09:09,680 --> 00:09:10,580 ‫into that file. 128 00:09:10,580 --> 00:09:16,610 ‫So you can do something like using a lines and then checking if they have a specific content. 129 00:09:16,610 --> 00:09:21,230 ‫So this is one of the methods to check for content. 130 00:09:21,230 --> 00:09:26,690 ‫And this is here line, which is the particular line that we're currently looking at in all of our lines. 131 00:09:26,690 --> 00:09:30,170 ‫And then we check if it contains the term third. 132 00:09:30,800 --> 00:09:35,090 ‫And if it does well, then please go ahead and write it and otherwise don't write it. 133 00:09:35,090 --> 00:09:42,350 ‫So we could create another text called Well actually try yourself create another text which is called 134 00:09:43,040 --> 00:09:51,200 ‫my text two and it should only write the lines that contain the two, which contain a two in their name. 135 00:09:53,440 --> 00:09:53,710 ‫All right. 136 00:09:53,710 --> 00:09:54,520 ‫I hope you tried it. 137 00:09:54,520 --> 00:09:58,570 ‫So this one and it's going to be my text, too. 138 00:09:58,570 --> 00:09:59,980 ‫And line contains a two. 139 00:09:59,980 --> 00:10:07,810 ‫And now let's write or let's run the code again and I'm going to close the straight away so you can 140 00:10:07,810 --> 00:10:13,390 ‫go to ah my text two, which is the new file and as you can see all the lines were written. 141 00:10:13,390 --> 00:10:14,200 ‫And why is that? 142 00:10:14,200 --> 00:10:16,960 ‫Well because all of them contain a two. 143 00:10:17,320 --> 00:10:22,600 ‫So if you would also want to only have the ones with a four, then you could only get second and third 144 00:10:22,600 --> 00:10:25,420 ‫into a file or written to a file. 145 00:10:25,420 --> 00:10:29,080 ‫So what if you want to add something to that file? 146 00:10:29,080 --> 00:10:30,700 ‫So add text to the file. 147 00:10:31,000 --> 00:10:34,810 ‫Well, then you can simply go ahead and go as follows. 148 00:10:34,810 --> 00:10:43,900 ‫So we're going to create a new line of code here called Using Stream Writer, 149 00:10:47,290 --> 00:10:50,470 ‫and it's going to be a file that I'm creating. 150 00:10:50,470 --> 00:10:54,610 ‫And that file should be a new stream writer. 151 00:10:54,610 --> 00:10:58,330 ‫And then here I give the path again like we had before. 152 00:10:58,330 --> 00:11:05,830 ‫So the argument is going to be a path and the second one is going to be a true so boolean. 153 00:11:08,380 --> 00:11:13,660 ‫What that will allow us to do is to write onto our file. 154 00:11:13,660 --> 00:11:23,380 ‫So file, dot, right line and I'm going to say something like additional line like that. 155 00:11:23,770 --> 00:11:25,150 ‫So now let's run it again. 156 00:11:27,970 --> 00:11:30,190 ‫And let's check out our file. 157 00:11:30,550 --> 00:11:31,800 ‫My text, too. 158 00:11:31,810 --> 00:11:38,410 ‫And we see additional line was printed to it so that you could go ahead and write all the different 159 00:11:38,410 --> 00:11:41,520 ‫scores that players have created, have made or had. 160 00:11:41,530 --> 00:11:45,360 ‫And you can file, write them all into a file or whatever you want. 161 00:11:45,370 --> 00:11:48,640 ‫You can write it into your file now, and that's pretty cool. 162 00:11:49,270 --> 00:11:49,660 ‫All right. 163 00:11:49,660 --> 00:11:54,910 ‫So now you know how to read and write files, which are text files.