Video instructions and help with filling out and completing Fill Form 8815 Filers

Instructions and Help about Fill Form 8815 Filers

What is going on guys welcome to your 81st Jeff torial and we are finally going to learn how to read stuff from a file so as you can already see I created this Chinese dot txt file and I added three more records to it so the first one is Bucky the second one is the ID number and then a name ID number name ID number name so it has four records and a record is pretty much um a row of data so now let's go ahead and figure out all right we learned how to open the file we learned how to write this stuff on this file but we don't know how to access it from a Java program so that's what I'm going to be teaching you today and the thing I didn't tell you is before we use scanner that scanner object to read from our keyboard using system in but we can actually use scanner to read from a file as well so let's go ahead and first make a private scanner variable so I'm going to name mine scan or X the cursor out of the way and now let's go ahead and just make a method to open the file first so public void open file and let's go ahead and put try to open the file so we're going to set X and again here's where we're going to put a new scanner and instead of putting a system in here which would be the keyboard just put new file and put that Chinese dot text I spell it right yep and now instead of reading from your keyboard it's actually going to read from this Chinese text file right here so that's a neat little trick that you can do and if we get any errors let's just go ahead and catch those errors exceptions e and now what do we want to do let's go ahead and write system.out.print line and let's go ahead and print something like good nods fine file looks good enough so now we have a method to open the file easy enough so what method do we need after this well this tutorial is all about reading files so let's go ahead and make a method to read the data from that file so public void just name it like read a file or something like that and what we want to do whooping cough the first thing we want to do is create a while loop and go something like this while I'll put your file name which is X and then we one-putt has next and what this is going to do it's a built in method it's going to make a while loop and it's going to keep going until it reaches the end of the file so it's going to loop through this and has next pretty much means if you hover over it on another input so as long as there's something there it's going to continue as soon as there's nothing there and it reaches the end of file this is going to be false and this while loop is going to break out so that's why we can work right while X has next it pretty much means while the file X has something in it so what we want to do is you see each of these records has an ID number a first name and a last name so we're going to want three variables to hold those string a and let's go ahead and copy that actually we're going to put string a equals X dot next and what this means is pretty much it's going to do this well let me finish writing all my variables that will tell you guys what does string a equals next and this is going to be B and C what it's going to do is this it's going to loop through the very first thing it sees and it's going to assign 20 equal to a then it's going to assign Buckey equal to B and Roberts equal to C and then we're we're going to put after this is we're just going to go ahead and print those out so system.out.print actually printf will be better so let's go ahead and print just three string variables so percent s space percent s space percent s in those variables of course are a B and C so now what it's going to do is after it gets those three it's going to print it out in might want to put a new line there and then it's going to go all right next 60 a Joe is B in Delta C now I'm going to print all those out and it's going to keep printing those out until it gets to the end of the file which is right here and then it's going to break out that while loop so that's actually the only thing we need in this read file method right there so now we just need that one last method and what this is going to do is close the file so public void close file just like that and all we want this to do is close that file X and now looks like we got everything check for errors and now we can go ahead and start putting it in our main so let's go ahead and put read file I didn't intrude probably yep R equals new read file don't take any parameters and now let's go ahead and put our open file r dot read file and r dot close file right there so now what this is going to do is call open file to open it read the stuff from it and close it when we're done with it so now let's go ahead and run this and we'll see what we got