so in the previous videos in this section of the course we looked at reading
and writing data using characters streams to write out the data we used the file writer
class and to read it back again we created file reader objects we then
discuss the idea of buffering the stream so that a large chunks the data are read into
memory by buffered Reader now this prevents excessive access to disk because
the data is only read from the disk when the buffer is empty
otherwise the file reader continues to take data from memory buffer so its much
more optimized compared to just reading a character at time now you
can also buffer data when it's written to disk
not surprisingly this makes use of a buffered writer class the concept is the
same the file writer puts the data into the buffed and the data is only written to disk
when the buffers is full and once again this prevents writing small amounts of data to
the disc which is very efficient because a disk drive generally speaking
is much slower than you know your computer can process so your computer applications is going to be
slowed down waiting for that disk drive to write data so by doing it this way
using a buffered writer data is written in sizable chunks rather than sort of
a character at a time now I didn't actually use a buffered writer in the previous
example and I only used the buffered reader to write the directions file the
data from the locations file was read from disk line by line and that
resulted in many accesses to the locations.txt file so in the next
few videos I'm gonna cover bytes streams which write binary data rather than
the character data to the streams and that would make sense to have an
efficient program to modify so I'm going to start off with a challenge so first we
are gonna remove all the commented out code to help tidy things up so got a good base
to start with so lets start commenting some of the stuff out so
and remember that in the locations file just above the static
initialization block we had this code here so let's just delete that clean
things up a little bit and scrolling down we also have this code here
when reading the exits lets delete that as well to make things cleaner
and I've also got this code down here which we can also remove
and that was our initial code to have directions locations and exits before we
decided to start writing that to a file....ok so that is looking better
ok so here is to challenge and I'm going to make it bigger so you can see what this is
all about and theirs two parts to this challenge so part 1....
....so
...so that is part 1 of our challenge
...now part 2...
....
...
....
...and the reason for the 0 quit is when the data was
originally written to the files it's been hard coded in the locations class
and their were no locations 0
because this is added by the location class constructor but now that we're writing the contents
of the class to the directions.txt file the 0 locations will also be save and
that's why a change will be needed to allow for this and this is that part
here I'm talking about the 0 quite exits before using the newly created files alright so
that is your challenge go away and see if you can figure that out now for this code and
once you have a got at it and you think you ready to see what we've come up with come back and
will go through that code so pause the video now
ok so how did you do hopefully you manage to get it working or go at it with a good go so lets go back
now and get this working so part 1 and just as a recap part 1 was to modify the
program so that it uses a Buffered Reader to read in the locations data and we're
gonna make that change and we are just gonna run to make sure things are working before we
move onto part 2 of the challenge so to implement the first part we really just
need to make a relatively simple change in the static initialization block in the
locations.java file so where we've go this try scanner new file reader
we really just need to change file reader to buffered Reader but we put the new
Buffered Reader here so...
.....so that's really all we need to
so we're now just change it so we added the new BufferedReader which
still takes to file reader as input but now the data will be read using the buffered
reader which is obviously more optimized to reading chunks rather
than reading a character at a time so lets go back to main and will run this to make
sure that it still works and theirs our locations so we can try typing up down east west west east
west north east north and quit
so that seems to be working right now
so lets move on to part 2 of the challenge so part 2 we want to modify the main method of the locations class so
that it uses a buffered writer to write the data and then we want to check the
locations text and txt and directions.txt to check the data has
has been written successfully and as I mentioned we need to make these changes to
allow for the 0 the quit exits before using the newly created file ok so how do we go about that
well let's look at location now at the moment you can see that we are using
a map a definition for map for exits for example here on line 12 and you can
see that we're using for the exits we are using a hash map so part 2 its gonna be really useful I think to
compare the newly written files with big locations and directions files and
to that end it would be really useful and helpful with the data was written
out in the same order as we read it in although this is not really necessary for populating
our locations and exits
map it will make comparing the files a lot easier now at the moment we
are using hash maps as you can see on the screen there which they have no guarantee
ordering of their keys and that is why in the hint
I sort of mentioned linked hash map in suggesting that you use those and the reason for that is the ordering in
will be the order that the keys were added to the map instead of just hash map which is
again no guaranteed ordering of the keys so to achieve that it's relatively
simple still we come back up here where we got this code if(exits != null so we change this.exits = new and we change this to linked hash map...
and we'll do the same for the next line as well...
and going down to the getter instead of returning a new HashMap
we want to return a new linked hash map as well so that's really the only change we need
to make in location but we also have to go back to locations.java so lets go and have a
look in there and we need to make a change in there as well
to use a linked hash map so at the moment you can see here on line 12 we're using hash map so
lets change that to a linked hash map and now within the main for the locations class
we need to make some changes in here as well but they are fairly simple as well so
the change we need to make here is we are going to create a buffered writer so....
....
......and that should be all
we need to do it all the other code their is going to be the same as it was before
we don't really need to make any other changes so we want to run this program
making sure by running the main within locations so let's just do that
run location.main ok so that is finish and then what we wanted to do is make sure that the locations and locations_
big.text are now identical so lets have a look at that so locations.txt
the one we have just written out and locations_big...
so theirs locations_big which are existed and then locations.txt which was
the one we just created and we scroll down randomly to have a look...it seems to
be pretty good but just double check
...look pretty identical and if we scroll down to the bottom....location 140 is the last one....locations 140 dead end....so I think that is pretty much identical...now if we close the locations file and look at locations_big
and directions.txt there are extra
entries for the quit option and we've added those with this code and now they've been added so direction_big
starts on location 1 but directions.txt we talked about that
0 extra entries for the quit options one for each location now there's two ways to
deal with this we could either stop the location constructor from automatically
adding the quit exit or we can change the code that writes out the data
so it doesn't write any q keys and as usually the case when programming theirs pros and
cons to both approaches here so if we prevent the location class from
automatically adding the quit option we are cluttering the data file with entries
that aren't really part of the game and there's a risk that someone editing the files
may forget to add the quit options for any locations they create but if instead
we change the program so it doesn't right out the quit exits then there's
really no need to remember to include them in the data file but we
lose the ability to have quit something slightly different certain
locations for example now if we wanted to add that function so the player had been lost in a
maze for long time for example you might want to offer them the chance to take them
out of it in exchange for some of their treasure if they quit so for my solution
what I'm gonna do is change the main method so that it doesn't write out locations 0
so lets go back and do that
so back in locations.java so at the moment we got the code their and you can see that
we are looping through
and we are using the for loop for the directions that we are just writing out everything but what we need to do
to ensure that we're going to miss this the quit option we basically add an if comparison
we are gonna put...
....
....so once we do that we
should now find that the directions file is identical so run this again and
just have a look at directions.txt we are now starting with one this
time and that's identical to that so if we scroll down to the bottom 396 is the last
line 396 so basically we got identical outputs from both by so that's good that we fixed that now
there's actually a third way that we could do with that issue and what we could
have done as we could have completely removed location 0 from the exits maps
and dealt with the quit option by then checking the user input in the main game
that would slightly complicate the code but it would save the memory used to store all
those extra exits now the memory of courses isn't a big issue with our hundred
forty locations but its always a good idea to consider all the options
before settling on a particular solution so that's the end of this challenge we've
now seen how to buffer both input and output in the next video we will start
looking at writing binary data using byte streams rather than the character
streams that we've been talking about today so see you in the next video