so now that we've actually seen the basics of handing errors by catching
exceptions were ready to have a look at input and output in Java now there's
actually several different approaches to Java I/O
the Java IO package has a bewildering array of objects dealing with
different I/O methods so in this section I'm gonna go through the most useful
ways of getting data into and out of your programs and also discuss the advantages
of the various methods as we actually encounter them and I'll also be looking at
the Java.nio package which was introduced in Java 7 now
what do I mean by input well input involves reading data from a source and an output involves
writing it to some destination now quite often the source and destination will be
files on the computer's disk drives but they can also be network connections and
pips as well as the computer's keyboard and screen and in actual fact we've already
used the scanner quite often to read input from the keyboard so strictly
speaking we aren't reading from the keyboard though we're actually reading from
system.in now that distinction may not mean much just yet but it will make
sense as we progress through this topic
also in the section I want to discuss dealing with disk files and directories
and the reason is that quite often you want to make a new directory to store
your data whatever is the user with the list of files to choose from and the
file class provides methods for doing things like that now there's also quite
a lot of jargoy and theory involved in I/O but rather than spending a lot of
time just talking I'm gonna actually introduced as we work through various examples
now there are some basics that I have to cover first out so here goes now I/O can be performed using
either byte or character data and the methods used are pretty much the same
for each its just the actual classes used that will vary so when reading and
writing character data the data is in a readable format and you can open the
resulting files if it is files that have been read or written in a text editor and you
can probably make sense of it but binary data on the other hand involves writing
bytes that will not look very meaningful if you open
the files in a normal text editor so which type should you use really depends on what the
data represents and you know if you're creating a report file to be imported
into a spreadsheet you probably choose a character string and similarly character
should probably be the correct type for writing XML or JSON data but once during your
programs variables or classes it may be more appropriate to use a binary format
it just does depend what you're trying to achieve now another distinction that needs to be
made between serial or sequential files and random access files now sequential
access can be thought of as a stream of data that arrives at your program
or is sent out from it in a defined order with each piece of data following in sequence now
random access on the other hand you know only really applies to files and allows you
to jump about the file or within the file retrieving or overriding any data in any
location within that file you choose so this is more like how a database program
would work in some sort of index showing you nowhere a particular record is
within the file so that it can be read without having read through
you now a thousands of the earlier records first for example so a compared sequential data to
stream in fact Java uses various stream object to deal with it and to see this in
action
will need to have some data to use so I'm gonna used the location data from the adventure
game example that we modified for the challenge in lecture 108 of section 13
now if you didn't complete the challenge then download the source code from the
Resources section for that video and then we'll make some changes and will
create a file to store the games locations so if we're gonna read the
location data from a file you know that really makes sense to create a separate class
to deal with that and to keep details of that implementation out of our main program
now the program currently works those so unless if their is a good reason to make a lot of
changes and we should really try and keep everything the same as much as possible but because the
main program uses a map to store locations one approaches is to you know create
a locations class that implements the map interface so what will do first is we'll
create a new class called locations and we are going to implement map based on the
location and we'll need a hashmap to actually store the
locations and then its going to use the code generator to actually generate all the various
abstract methods that it needs to do and we can use IntelliJ to do that so
go and grab the location or grab the files from the lecture 8 of section 13 because
will need that in order to proceed so I'm gonna pause the video now come back
when you've got that and will actually continue so what I'm going to now is import
the locations class and also the main class from that project the one we talked
about earlier
so to do that I am going to delete the main class because we're gonna be importing
its so I'm gonna delete that and swing over to the folder where I actually downloaded
the source code from that lecture again
section 13 which was lecture 108 so if we go back and have a look at that I've got this downloaded and
I'm gonna unzip that and I'm going to go into the files and theirs 2 their com.timbuchalka
location.java and main.java and that are the files that we work on
so I'm going to take a copy of those files I am gonna copied them so now I've copy those I'm
going to actually go in back into the project then right click it and click on
reveal in finder have a similar options if your under Windows or Linux and go into that folder
which in this case I've called it InputOutput into the source folder and go into my
package whatever I called it I'm gonna paste those files in their and so when I go back to
IntelliJ now they should actually appear so if I go back to IntelliJ now they should actually
appear in their so we know got location which is the location file for that challenge from lecture
108 and also the main class now if you doing this and your importing
your own project make sure you got the right package name
you might have to fiddle around a little bit to get the right package but in my case
the com.timbuchalka was the package used for the original challenge
and also for this file for this project so consequently things work fine ok so
we've got those two files in that we're ultimately gonna be using we need to create a
new class and this new class is called locations and its gonna be used to sort of
map the actual location or each one of the locations we got for this now the original main
program used a map or uses a map to store the locations and the approach
we are gonna take is as I mentioned we are gonna create a locations class that implements that
map interface and this is going to basically allow us to place all the
initialization code into the new class and not have to make any changes in our main
method so lets go ahead and do that now we are gonna create this new class new Java class and call it locations and we're
going to come over here and put.....
....
....and we need to create an actual locations
object to store our locations so....
....
...and hashmap is obviously going to store the locations and I'm gonna use
the auto generate to actually generate some code so if I do alt N we actually want to implement the various methods that
are part of maps so I'm gonna click their and see how they are all selected for us
automatically we got quite a few of their size is empty
contains key etc we need to select all of those or that was selected for us automatically to
implement them press ENTER and notice that on line 11 now we don't have any errors
anymore but now we need to actually do something with this code because obviously
just a default code that has been generated and won't have code that we need
in their so firstly the location the size is going to return
how many elements are in our hash map so....
now to determine whether its empty instead of just returning false we're gonna actually return
locations .isempty so that return true or false depending on whether we got any
items or any location objects within our hash map so contains key we are going to use the code
straight out of the hash map so put....
and whatever the name of the keys that the objects has passed to us and likewise for contains value
we are gonna do a similar thing.....
.....
....
.....
....
.....
and that should probably be the only change we need to make so as you can see we're
using a HashMap to store the location just like the main class did and all the methods
that we have to implement really just delegate to the corresponding methods of
the hash map as I mentioned we aren't gonna use all of them but when implementing an
interface of course we have to implement all the abstract methods of that interface and
there wasn't that many anyway and it didn't really take a lot to set them up but
it's really worth the effort to encapsulate the location data like this
by doing it this way so what we just created is a class that now pretty much behave just like a map but
which we can customize to load its contents from an external source a disc
file in this case so we can now swing over to the main class and start replacing
the locations map with our new class so let's go ahead and do that so in here so firstly
now on line 8 we've got a reference there to creating a new HashMap we can
actually change that to a locations object so lets go ahead and do that so that is going to be
.....the new class we just
created earlier in this video and of course that needs to be static as well so make that a static....so now
we've actually done that we need to remove a single bit of code and
scrolling down i think its on line 60 and originally I put that line into
to demonstrate the problems with exposing with our exits to an external class we
don't need that anymore so I'm gonna get rid of that just to keep things tidy so I'm
just gonna remove that line and now if we look at our class we should find that
were pretty well manage to slot in our change to our new class and it
seems to be working fine but certainly there's no compilation errors which we are off to a good
start and that's really all the changes we need to make tp main now we can
actually cut out all the code that initializes the locations and paste them
into the new locations class so let's go ahead and do that so start on line
21 here this is the code here we use to create these locations
copy right down to the end of that I think was the last one their locations
cut that clean that up and go back to our locations class and we are about to paste that in
but there's really only needs to be single instance of the location
data for the game so it doesn't allow it doesn't make much sense to allow
multiple instances of the locations class to each of their own copy of the
data now we saw in the lecture about the static keyword that there's only one
copy of static data that is shared amongst all classes instances so here it actually makes sense to
keep this location data static and I also discussed static initialization
blocks and this is actually a good time to actually use one so lets create that static
initialization block in this locations class go ahead and do that so let me put that in
their so...
and just a reminder the static initialization block will be executed once and that's when
the locations classes loaded
and other classes a HashMap field called locations that you can see on the screen
there and that's also static so theirs really only one copy of that shared by
you know how many instances of the locations class that are created
now there's no real reason to create more than one location object
but as we can't make the interface method static we have to allow at least
one instance to be created and of course once you do that you can't guarantee
that someone else won't create one more than one of them's now there are ways to
ensure that only a single instance of the class can be created the singleton
design pattern is probably the simplest here but we'll look at that later in the
course but for this application making the data static and ensuring its only
initialize once is actually fine and using the static initialization block
confirms that actually is the case so lets just run this to make
sure that it did work as it did before
make a bit of space here so we can see what's going on starting at the end I can go north go south east west
quit and that is all working fine so the main class really doesn't care that
is now using our locations class rather than a hash map that it used to use because
we've implemented the map interface in all the required methods so I'm gonna end
the video here now but on the next video we are going to take this to the next level we are
gonna start looking at getting this data into a file or reading from
a file rather in the way that we are using it now which is as you can see here on lines
15 and sort of going down we are actually hard coding it so let's look at doing that
in the next video