so lets actually run this again now and see what actually happens so when it
runs we get actually nothing printed and that's because there's no finally
block to put a print statement in it was only printing messages to show that
the blocks are being executed anyways so you wouldn't ordinarily do that anyway now you
could still add a finally block if that's what you wanted to do but don't try and
close the resources in it because that's unnecessary and closing some resources
when they are not open can cause an error so you leave it to the try
resources block to do that for you automatically so now you can see that
what we've got here is and I'll just close down the run window it's a lot tidier and a lot more
concise we've only got a few lines of code there including the try so as much
tidier and now we've done that let's add some code to also write out the exits for each
location now using the try with resource forming the try block actually
allows more than one resource to be specified and as a result we can
actually create a second file writer to write the exits data out now as the
code already loops through the locations what I'm going to do is add an
inner loop to write the exits for each location rather than
having two separate blocks of code so let's go ahead and make those changes so the
first thing we need to do is add a second file writer object in our try
initial try and the way we do that is we actually put a semicolon on the first one
we go to a new line and put....
....so that's how we actually put
2 file writer objects in the initial try resources section and what we are going to do
after the initial write of the location we are gonna cycle all through all the
directions or the exits for this location and write those
out so we can do that....
...
....
...
.....
...so thee location has been written first
and then we do a for loop for that location going through all the
exits for that given location and actually saving those to the file as well
which of course is the direction.txt file so let's actually run that now
to see if it works in the left hand side you may have seen the directions.txt file
getting created so there it is there and there's our various directions now it looks like
there is something that we need to fix their we go back to our code and have a look
at that again and actually there is something I need to change what I need
to do is to put another comma in their so looking at that code we really need to differentiate
that and we got the original location then we got the direction in which exit which
location it leads to and I didn't put a comma their so it's going to lead to problems latest so I'll put
another comma in their and that would make it correct this time and also be bit easier to read so if we have a look
at the directions.txt file now you can see we got commas between them so that's good so that is all working
so its simple way to write data out to the file now I should point out at this point that from
a purely I/O point of view theirs probably better ways to store the data for this
game so writing it to a text file like this really isn't very efficient but
with that said their can be advantageous to storing data in human-readable format
like this and that's because we can edit the data files directly if we wanted to
like for example to add you new locations and exits would be quite easy and just open
this up in any text editor and making those changes so at this point I'm now going to
switch
over to looking at reading data so we've created the data files from
the game needs and it's now time to use them to initialize the games location
and I'm gonna start by commenting out the code that's currently in our static
initialization block so open that up close that directions.txt so I'm in
locations and what we need to do is make some changes now to the static
initialization blocked since its the code that we place in here a few
videos ago so what I'm gonna start by doing is going to comment out all this code that's currently in
here and we are going to ultimately see if we can read this data back in
these files that we've created so lets comment all these code out in this
static initialization block so comment that out and go back to the top and start adding some code so we are gonna use file reader objects
to get data from the two input streams namely the locations and the directions
text files that we have created and the process is very similar to what we're
just done we've written out the data just the other way around the main difference is that we
need to pass the data into the individual strings and integers so we're
going to use the familiar scanner to do that for us now so that you can see how
it's done before Java 7 I'm gonna start once again by using try finally and then
ultimately we are going to convert the code to using the new try resources method
so lets do the long way first so let's start with scanners....
.....
..and we are getting an error at the moment because we haven't given the try block
anything to throw an IOException so let's change that now so....
....
...
..this is our way to tell the scanner that our information is separated by a comma
so the key piece of information location etc the
description of the location or wherever it is that the actual fields are
separated by a comma and we do a while loop....
.....
...so the first bit of information was the location so we are gonna
retrieve it into an integer because of course it was an integer next we are gonna do something
you haven't seen before so...so you can
guess what that's going to do that's now gonna skip over the comma to move
to the next section and again just very quickly
locations.txt you can see we've got a location we've got a comma and we got the
descriptions so we are retrieving the location we've now moved past the comma which
was the delimiter we're going to continue onto the actual description and
the way we do that we just put....
.....
...just so we can see its importing it correctly we wouldn't normally
put that in your code testing purposes that is fine and lets now create a locations
object so.....
.....
....
.....but for now you could see that we've actually
written some of this information so basically the code looks very familiar
and very much along the same lines as writing data to an output stream as you
can see there if you remember from a videos ago but of course instead of the file writer
object we are using a file reader and we are passing it the name of the file
as constructor and that is the file that is ultimately going to get the data from
file reader stream is then passed to the constructor of a scanner
causing the scanner to work with the data in the stream in the stream
itself rather than system.in which you have seen previously in the course to
read for the keyboard so obviously it's a different stream as though the scanner
still operates the same in that its retrieving data but we have set it this time
grab it from a file instead of a keyboard now I could have declared a new file reader object variable and pass that
to the scanner but you will find constructors chained together like this
a lot when performing I/O operations in Java
in fact the classes are designed to work together in this fashion so creating a
new file reader object like this instead of actually creating separate
variable and passing
the variable now to see an example of how the they are designed to slot together will
actually I'm not gonna tell you that right away because that is going to make a good mini challenged o here is your mini
challenge why is the file reader object not closed despite me saying that
closing streams are very important and a hint here is to remember that you can
view the source code for Java objects and methods by control or command
clicking on the method see if you can figure that out and come back come when
you ready and we will go through and solve this mini challenge so see you when you get back pause the video now
ok so hopefully you figure out what's going on
or the reason why the file reader objects is not close despite me saying earlier
that closing streams is very important for the reason is that when the scanner is
closed its close method also takes care of closing any stream that it was using
provided that the stream object implements the closable interface and
the file reader does do that so shouldn't really have used to terms stream here
it's probably more accurate to referred to it as a readable because the source for
a scanner must be an object that implements the readable interface so if we
have a look at this we are gonna look at the scanner close method so lets go into the scanner click on that
and if we have a look at its close method
do a search for close you can see that it test the source whether it's an instance of closable you can see that
on line 1091 and if it is it causes the automatic
calls the sources close method if it is so consequently we don't need to worry
about closing this file reader object because it's automatically handled by
the scanner class itself when it's being closed down and if we go back to the code and
we are calling scanner.close on line 61 as you can see there so its pretty cool how that's
handled automatically for us so remember that a class can implement more than one
interface so an object can be an instance of a close and a runnable at the same
time and that sort of really really important so reading the
directions text file to build up the exits for the location is very similar
to reading the locations
but I'm gonna do something slightly different their and introduced Buffered Reader so lets
look at doing that in the next video