In this project, we will be working with streams and files. We will use the code from Project 2 (recall the Animal class and its subclasses) to read and write our Animals from and to a file (a database of animals). In the end, we will have an interactive program that will prompt the user for selections to modify this Animal 'database' that we will create. Basic functions like Add, Delete, List and Select will be impelmented (also exit and re-write the database!).
The objectives of this project are to use streams in Java, including reading in info from standard input and reading/writing a binary database file. More practice with exception handling will also be done (as run time errors frequently happen when working with file I/O).
The first step in this project is to go back and copy your classes from project
2 to a new directory so you can add some new methods to them. Namely, you
will be implementing readAnimal() and writeAnimal()
methods to read and write
your animals from/to a file. This should be done by using the
DataInputStream and DataOutputStream classes.
The readAnimal() method in each class should take a
FileInputStream object as a parameter and use methods such as
readUTF(), readDouble(), etc. to instantiate a new
object from a file.
The writeAnimal() method in each class should take a
FileOutputStream object as a parameter and use methods such as
writeUTF(), writeDouble(), etc. to write out the
object values to a file.
Remember to practice good OOP techniques, including code re-use;
meaning that you should be using the super class to take care of elements in
it (as we did with the constructors and toString() in project 2).
After this, you should
write a small test program to write a few Animals to a file and then read
them back in. Remember that we will have many different animals in one
file (for our database), and that this will be a binary file, not text.
After getting your Animals to and from a file, now create an interactive
program to do this for you. The program needs to accept one argument, a file
name for the database. The program should first read information from the
database (provided that it can, and if the file has the proper information!).
Then the program should enter an interactive mode where you can enter
information to the database. To be more specific,
a sample running of the program to show the features required:
Sample Run
Here are the functions the program must do (Formally):
readUTF() to read that in).