C# Tutorial 17 File I/O

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
well hello internet and welcome to part 17 in my C short video tutorial we're going to be covering a ton of things in this tutorial we're going to first off cover the file system file directory info file info file stream streamwriter screen reader binary writer and binary reader so a whole ton of file i/o and like always all the code as well as a transcript of this video is available in the description underneath this video and I have a lot to do so let's get into it okay so here we are and you're going to need these two libraries right here to be able to have everything work and you also may need to go into Windows and here's Visual Studio 2017 or you can use the Marin whatever you want to use but you may find that you need to login as administrator how you do that is go to more and then run as administrator okay but that's all for that and everything should work the way I have everything set up here now what I'm going to do first off is talk about how to mess around with directories and if you want to get access to your current directory what you can do is go directory info and let's just call this current directory and you can just go new and directory info and then inside here just put a dot okay so that's your current directory now if you want to get access to another directory you're going to do it the same type of way you're going to go directory info and let's say Derek's directory is equal to new directory info and then so that you will be able to use the back slashes you're going to put an app sign inside of here and then you're going to just point to whatever directory you want to point to so I'll go to my favorite directory which is the Derek Mann structure II end-users and everybody should have one of those so what else can we do well we can get our directory path so let's come in here and let's output that information so you just go Derek's directory like that and full name and that's going to get you that and I'm gonna output all this here in a second we can also come in here we can get the directory name for this and to do that you're just going to type in name and what else can we do we could also get the parent directory for this so once again Tarek's directory and then parents loops make sure that's capitalized parents like this and what else we could find out what type of file we're pointing out here so we can go once again Dericks directory and attributes and it's anything else when it was created I mean we can get all kinds of things I'm just covering some of the things you may actually need and don't worry we're going to get into writing files and reading files and all that here in a second just want to cover this completely and that's for the most part everything so let's run that just to see what that looks like and you can see right there all the output that we have here for that guy okay so pretty cool stuff so let's go and let's do a little bit more let's say we want to hmm widths create a directory we go directory info all once again directory info and I'm going to call this a data directory and equal to new directory and veau and then once again the at for the backslash is C colon and users and I am going to put this in my Derrick vans directory of course put it wherever you want and I'll just say c-sharp data just to do something okay so that's going to create a directory for us and then if you create a directory of course you're going to want to be able to delete it and to do that you just go directory and whoops the directory info dot delete like that elite and then you're going to throw in whatever you want to delete so just the same exact thing you have right there paste it inside of there and there you are you went and created a directory and then you delete it one all right and up next I'm going to get rid of this actually because I want to use this directory in my other examples that I cover here I'm going to show you a very simple way to read and write files so what we're going to do is we're going to create a string right here and I'm going to write it to a file so I'm going to go customers is equal to and let's throw a whole bunch of people in here my favorite person Bob Smith and his sister Sally Smith and one more how about Robert Smith okay so there we go we have our all of those people hopefully I don't make the Smith family upset with me so now what I'm going to do is well I'm going to create a string to a file so I'm going to go file path it is equal to this is just the save time and then I'm going to let's go up here and just grab this guy right here so I'll type all that out again and paste out inside of there and then at the end of it I'm going to call this test file 1 dot txt and then I'll just be able to refer to that instead of you know typing all that out over again so if I want to write the array to a file I can just go file dot and write all lines and then pass inside of it the file that I want to write to so that's going to be the text file path and I'm going to follow that up a width of the array I want to write to it and it's going to be customers there we go you just wrote to a file now if I want to read strings from that guy I can just come in again and go for each and I'm reading strings and just call this cust in and I can just go file read all lines like that and pass in what I want to read from whoops I can spell it right text file path is what I'm reading it from and then I can output all of that information on our screen so I can just go and CW tab tab is creating that at the shortcut and inside of here I can say something like customer than just customer like that and then the customer itself and there we are and I can run it whoops what did I do wrong line 42 oh okay I forgot to put the dot inside of there not a big deal and now we can run it and you can see that it wrote that information and then it read it back out okay so pretty cool stuff so that's an easy way to read and write files and now that we did that why don't we go and get a little bit of information about our file so I'm going to go and do directory info again and I'm gonna call this my data directory is equal to new directory info and inside of here let's just go up here and copy this again to see data like that and paste that inside of there then we got that now let's say I want to get all the text files that are in that directory I can do so so I'll just go file info and I'm going to call this text files and just going to store all those in there so I just go and point to my data directory again and get files that are going to match the format of pretty much anything followed with the extension text and then you just type in search option all directories and that is and then we can come in and we could output the number of matches that we received so we can say it matches and then follow that up with the just calling for text files length on that iraq and that's going to give us that and let's also come in here and output information i only have one file inside there but it would get all of them and to do that i just go file info file in text files and after we do that we can go and output some information say we want to get the files name we can do that and we could also get the number of bytes in our file and to do that we'll just say file and link and if we're on nuts doc tada you can see oh I actually have a couple files inside of that there's text file 138 bytes text file 223 text file 355 and there's the number of matches and it's so cool stuff and now what I want to do is talk about file streams now a file stream is going to be used anytime you want to be able to read and write a byte or an array of bytes so what I'm going to do here is create a path to a text file so I'm gonna call this text file path to is equal to and at do I have anything saved inside there yes I do look at that so I'm going to call this test file - dot txt and then let's open or create a file if it hasn't already been created and to do that you go file stream I'm just going to call it FS for short and then you go file open and then you're going to pass in the file that you want to open so that's going to be you back out right there and then afterwards I'm going to say that I want this to be created and I could just say create like that and there it is it opened and created a file for me now I want to save a string inside of here remember I said this stores bytes or a byte array well how you can do this is we can come in here and do something like this is a random string and what I'll need to do here is for I store it I need to convert it into a byte array how you do that is you just create a byte array and call whatever you want I'm going to call it RS byte array and then you go in coding defaults and get bytes like that and then inside of this guy you're going to put inside of there what you want to convert which is going to be our string that we're going to be using and then if we want to write to the file but what we're gonna have to do is the find the byte array as well as the index to start writing from so we'll go file stream right and we're going to define the byte array that we are going to be working with and then the index we want to start which is at the beginning which is zero and then we're going to get the total length for it so RS bytes array and then follow that up with length obviously all right so we got that all set up now what we're going to want to do is if we're going to want to read data from it we are going to change the file streams position back to the beginning of the file stream and then we're going to create a byte array to hold that file data so let's just create one here and call this file byte array we're going to be reading that back out of there new and this is byte array obviously and it will put in the link for it because we know what that is and we can get it by calling length just like that all right so pretty neat stuff of course all the codes in the description if I'm moving a little bit too quick for him so now what I want to do is put the bytes inside of our array so that we can manipulate it see what's inside of there so I'm going to start at the zero index and we're going to keep looking for stuff as long as the byte array length or the Deadeye is less than the byte arrays length and then we're going to increment i whoops would it do went crazy there for a second I plus plus and then inside of this guy we're going to go file byte array and throw I inside of there and then we're going to convert this to a byte and FS and we will read bytes so there's read byte get that all there and then after we have this new byte array we just pulled it from the file we want to convert from bytes to a string and output it and we can do that quite easily by just going encoding once again and default once again and here we're going to say instead of get bytes we're going to say we want to get string and we are going to use the file byte array obviously and we will output that and what I do forgot parenthesis that is made happy and then of course after we are all done using the file stream we're going to close it so let's run it and let's see it work and you can see that it wrote that file to or wrote that string to the file and then received it and I'll put it on the screen okay so that's how we use Fosters so now what I want to do is talk about how we use a stream writer and stream reader now stream readers and writers are going to be best for reading and writing strings so what I'm going to do is I'm going to create another text file path I'm going to call it three is equal to at and I save that yes it did good for me and then we go just file free and we're going to be using a text file this time and then after that guy is grated we want to create a text file with our stream writer this time so I'm going to go stream writer just call it SW for short and let's then go file once again except here we're going to go create text and then we're going to pass in the new file that we just referenced there then if we want to write to a file without a newline we can just go write like this and then we could say this is a random and put a space at the end there and then if we want to write to the file with a new line at the end of it we say write the line like that and then we can put sentence in there or do whatever we want okay save that we can of course keep putting additional data inside of there so let's go and say something like this is another sentence this is another sentence and that's good enough and now all if you want to close it just call close on it and it's been closed and now let's open the file for reading and we're going to use a stream reader this time it call it s are equal to then we'll go file open text once again and text file path three is the text file that we want to use here and there's a couple of different functions we can use why don't we take a look at peek which is going to return the next character as a Unicode number and we can of course then change it into a regular old character if we'd like so here I'm just going to say peak like that and then I'm going to go and let's quotes off and let's go and call it and convert it from a unicode number into a regular character and to do that we're going to use our old pal converts and then change it to character and then we'll go SR Peak like that we're also going to be able to read up to a new line so let's go and just output that on there as well and here we can do something like first string oh it's also good to know that peak is going to return that value but it's not going to it's still going to stay stay at the beginning of the file so that's something else that's important to know and here what we're going to do is get to the very first string up to a new line do that we go read line lick them and then we can also read to the end of the file altogether so read the entire file in here and here we will just call that everything which is going to be everything that's left that we haven't already read and to do that we go read to end and of course after that's all done we want to close the stream reader and of course you guessed it we use close okay I'll set up and let's run it again and there you can see peak weight and grab the first tee off of there and there is that tee but it didn't eat it you know so this didn't come back as his is a random sentence that came back as this is a random sentence and you can see it ended because there was a newline there and here grabbed everything else that was left okay so that's how you use the stream readers and the stream writers and now I'm going to take a look at the binary writers and the binary readers okay so the binary writer and reader are going to be used to read and write data types which is I'm sure you can tell pretty useful so we're going to go and create a new file here again path 4 is equal to at and isn't still there no it isn't okay whoops everything's going haywire all right let's jump up here and with this guy right here and copy it and then come back down here to our happy place paste that inside of there then let's change that to a four and we're going to use a DAT file here in stud so there's the dot file and now we are so that's the new file we're going to be working with now if you want to get the file we can just go info that file is equal to new file info and then you're going to reference the actual dot file that we just referenced there and we can open the file using binary writer like that I'll just call it BW and that is going to be equal to the new binary writer of course and we're going to use our dot file and we're going to say open for writing and we're going to create just some information we want to store inside of there so I'm going to create some string called random text and it's going to contain random text you guessed that all right and then we're going to create another one called my age and make that equal to 42 and then we'll create a double which is going to be height and then have that be 6.25 and got all that set up now if we want to write that information we just go BW and write and let's get some random text and throw that inside of there and let's copy this because we got two more to do and let's go my age inside of here and also height so there is height okay so we have all that setup that's going to write those out and we can then come in here and close our writer our binary writer we can then come in and open a file for reading and we're going to use the little binary reader for that I'm going to call it gr is equal to new binary reader and so I'm going to reference the DAT file again I'm going to say this done but I want to open it for reading however and then if we want to output the information that's stored inside of there we can do so going to the binary reader and say that we want to read a string from it because we know we stored one of those inside of there and we got two more datatypes to read out of here so let's also come in here and say we want to retrieve the integer and let's say that we also want to come in and retrieve the double and then after we're done with that we can close the reader because we don't need it anymore and got that saved then let's run it and there you can see we went and grabbed random text the string 42 which is the int and the double which was six point two five okay guys so that is a lot of information on how you can read and write files and a whole bunch of other different things and like always please leave your questions and comments below otherwise till next time
Info
Channel: Derek Banas
Views: 96,326
Rating: 4.8072624 out of 5
Keywords: C# Files, Working with Files C#, C# Saving Files, C# Reading Files
Id: HKqMqFJr4SY
Channel Id: undefined
Length: 20min 44sec (1244 seconds)
Published: Sat Mar 04 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.