Name______________________________This lab covers some of the important ideas related to Java IO. You should print a copy of this lab so you can record your answers to questions. Turn in the completed questions and any supporting printouts as required in the lab. Here is a link to the API documentation in case you need it.
To get started with this lab, download and extract the folder in the zipped lab archive. This contains source files for the lab.
Text File Memos
Create a project named Memo using the 4 files found in the Lab10 folder. The first part of the lab focuses on just three of the files: Memo, MemoBook, and MemoTest. The MemoTest program is to allow memos to be created, stored, and recalled. Currently, it allows you to create memos, and then displays the Memo Book on the screen. (Be sure your project can do this before going on.) Notice that a MemoBook extends ArrayList... that is, it IS AN ArrayList! Look at the toString method of MemoBook and think about how inheritance is being used.
Each time you run the MemoTest program, a list of memos will be created in memory. When you cancel the memo creation dialog, the memos will be displayed in the console and then discarded as the program ends.
Your first task is to change the program so the memos are stored in a text file. You should do this by uncommenting the code in the partOne method that calls the saveMemos method of MemoBook. Note that it is passed the filename of the file to be created.
Complete the saveMemos so all of the memos are written to a text file. Each memo uses three lines in the file: topic, timestamp, and memo text. (Hint: look at the toString method in Memo - do not look at toString in MemoBook).
Test your program by adding three memos of your choice when you run TestMemo. End the program (cancel the memo dialog), then open the data file in a text editor (You can use Eclipse, JGrasp, or Notepad). Verify that the memos are stored correctly in the file. Did you remember to close the file? If not, add that statement to the end of saveMemos and redo your test.
Each time you run the program, the old data file is erased. Check the FileWriter API to determine how to create a FileWriter object that will append new data to an existing file rather than overwriting the old data. Make this change to your program (to append to the file). Add another memo and then verify that the old memos are still in the file, and that the new memo was added to the end. What change did you make to the program to allow appending?
Answer:
Next, we modify the program so it simply reads memos from the file and displays them. You will need to complete the MemoBook method, readMemos. This should read memos using a BufferedReader connected to the file (the filename is passed to the method). The memos need to be created from the file data and added to the MemoBook object. Before you write anything, look carefully at the Memo class. There is already a method there that you may use to read each Memo. This will make readMemos very easy to write! Look carefully at what it needs and what it returns. Note that it is a static method. For each Memo read, you should call the inheritedArrayList's add method to actually add the memos to the book. Note that the readMemos method must return an int, the number of memos actually read. You will need to deal with exceptions in an appropriate way. For example, if the file is not found, you should return with an empty MemoBook.
To test the readMemos method, change the main method (in the MemoTest class) to call the partTwo() method rather than partOne(). Uncomment the line in partTwo that calls readMemos.
Test that your program correctly reads all of the memos from your data file. They should be displayed on the console. Edit the data file (using your text editor) to introduce 2 illegal memos: one with an an illegal date, one with an empty line for a topic. If your program crashes, you need to spend more time handling exceptions before going on. ReadMemo should probably display errors on the console and then go on to read more memos. Run your program and tell what happens.
Answer:
Serialized Memos
In this part of the lab, we will use serialization to store a memobook object in a file. Open just the file named SMemoBook - a serialized version of the MemoBook program you just worked on. This program inclues a test harness (main) that will store and read memos in a binary file, memo.bin. The program starts by listing the Memo topics already in the file, then allows new memos to be added. When finished, the memos are listed on the console. Well.. it will do this when you are finished!
Your first task is to implement the file storage! We will do this by serializing the SMemoBook object. What interface must a class implement if it is going to be serialized?
Answer: __________________________________
Add the correct implements clause to the SMemoBook class. Our first task will be to implement the close method of the SMemoBook class. Its job will be to serialize the SMemoBook object into a file. Instantiate an object of the correct type inside close and use its writeObject method to send this (the object itself) to the file. Be sure to close the file after writing the object.
Run the program (be sure there is at least one Memo in the MemoBook) and tell what runtime error occurs when close is called. The error will mention the Memo class.
Answer: _____________________________________________________________________
To serialize an object that contains other objects, all of the included objects must be serializable. Add an implements clause to the Memo class refered to in the error and try the program again. Verify that the binary file is created when the program exits. Feel free to view it's contents (but keep in mind it is a binary file, not a text file).
Answer this question... What types of objects are contained inside each Memo object? Which of these are serializable? How do you know?
Answers:
Each time main runs, it instantiates an SMemoBook object, but it always begins empty! We want to instantiate a memo book from the file that was saved the last time the program ran. This presents an interesting problem. Currently, we are getting our memo book using new SMemoBook(somefile); But the deserialized object will already be an object; we do not use new and do not use a constructor when deserializing. In this situation, we DO NOT WANT TO USE A CONSTRUCTOR! The object will be created, filled, and returned to us when we use the readObject method.
We need to finish the public method in SMemoBook to do the deserialization for us. Finish the heading for getSMemoBook in the SMemoBook class. It must be public. It will return an SMemoBook object. It requires one argument, a File object. You must also replace the statement that uses the constructor in main with a statement that calls this new method and stores the returned value in the variable book. However, in main, there is no SMemoBook object yet in existence, making it impossible to call the method. That is, unless you declare the method to be what? Hint: Call it like this - SMemoBook.getSMemoBook(... )
Answer: Add the keyword ___________________ to the method.
There are several different exceptions that must be caught.
Every exception is handled the same way, so only one catch clause is needed. Complete it to catch exceptions from the class Exception, display the message from the exception object on the console, and then use the SMemoBook constructor to assign an empty memo book to smb.
Test that each time you run the program, it retrieves all of the previous memos and allows more to be added. When you see that this works, try the fiollowing experiment:
1) Add a private int data member to the class SMemoBook. Run the program. What exception occurs and why?
Answer:
(Remove the int member from the class)
2) Erase the file named memo.bin from the disk and run the program. What exception occurs and why?
Answer:
Why would it be a good idea to change the SMemoBook constructor from public to private? Hint: Consider the consequence of using the constructor from another class... what might be lost?
Answer:
When finished, get printouts of the following three files (be sure your name appears in the files): MemoBook.java, MemoTest.java, SMemoBook.java. Submit your answers and program solutions in a single stapled package.