Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I wanted to have someone look over the two methods I wrote below. Both are serializable methods. It is the first time I have written

I wanted to have someone look over the two methods I wrote below. Both are serializable methods. It is the first time I have written them and want to make sure they work/are correct. I was having particular trouble with the first method and the OIS.readObject(st.size());

public ArrayList loadPlants() { ArrayList plants = new ArrayList(); plants.add(new Plants("JAPMA", "Japanese Maple", 30, 60.25, 85.50)); plants.add(new Plants("DOG", "Dogwood", 20, 15.50, 32.50)); plants.add(new Plants("RED", "Redbud", 45, 21.00, 28.00)); plants.add(new Plants("RBUCK", "Red Buckeye", 24, 21.00, 33.00)); plants.add(new Plants("CRMY", "Crape Myrtle", 48, 19.00, 29.50)); plants.add(new Plants("TULIP", "Tulip Tree", 30, 23.00, 38.50)); plants.add(new Plants("FALSE", "Hinoki False Cypress", 40, 18.35, 29.50)); plants.add(new Plants("SERVICE", "Serviceberry", 30, 43.00, 58.50)); plants.add(new Plants("SMOKE", "Smoke Tree", 18, 34.00, 45.50)); return plants; } public ArrayList readSerializable() // menu item #1 { // create the ArrayList ArrayList st = new ArrayList(); Frame f = new Frame(); //decide from where to read the file FileDialog foBox = new FileDialog(f,"Reading serialized file", FileDialog.LOAD); foBox.setVisible(true); //get the absolute path to the file String foName = foBox.getFile(); String dirPath = foBox.getDirectory(); File inFile = new File(dirPath + foName); // create a file instance for the absolute path ObjectInputStream OIS=null; try { FileInputStream IS = new FileInputStream(inFile); // create a file input stream for the file that we chose OIS = new ObjectInputStream(IS); // create the object imput stream st = (ArrayList) OIS.readObject(); // note that you can read in the entire object (the array list) at once // now read in that extra piece of data that we wrote out and set the next customer number to use //OIS.readObject0(st.size()); } // catch any IOException that occurs catch (IOException io) { io.printStackTrace(); // great for debugging! System.out.println("An IO Exception occurred"); } // note that you can also have a class not found exception. catch (ClassNotFoundException cnf) { cnf.printStackTrace(); // great for debugging! System.out.println("An IO Exception occurred"); } finally // finally always runs no matter what so close the file here! { // close the file. Java is neurotic - it worried "but what if it is already closed?" so needs another try/catch try{ OIS.close(); } catch (Exception e) {} // note the {} - means "do nothing". I wanted it closed anyway. } return st; } public void writeSerializable(ArrayList st ) // menu item #2 { // create the frame for the FileDialog box Frame f = new Frame(); //decide where to save the file FileDialog foBox = new FileDialog(f,"Saving plants file", FileDialog.SAVE); foBox.setVisible(true); // we need to get the path where the file will be stored. // the user will pick one from the dialog box // the combination of the directory name plus the file name is the absolute path String foName = foBox.getFile(); String dirPath = foBox.getDirectory(); // create a File instance for that absolute path File outFile = new File(dirPath+foName); // create a PrintWriter FileOutputStream OS=null; ObjectOutputStream OOS=null; try { // create the FileOutputStream object OS = new FileOutputStream(outFile); // create the ObjectOutputStream object OOS = new ObjectOutputStream(OS); // create your output string OOS.writeObject(st); // write the entire array list out at once. COOL!!! // we also want to save the next customer number // now print the next customer number that should be used to the file also // need to keep track of this so that when you read the data back in, you do not reuse // customer numbers //OOS.writeInt(Plants.num); OOS.writeInt(st.size()); } // catch any IOExceptions that occur catch (IOException io) { io.printStackTrace(); // shows any errors System.out.println("An IO Exception occurred"); } finally // finally always runs no matter what so close the file here! { // close the file. Java is neurotic - it worried "but what if it is already closed?" so needs another try/catch try{ OOS.close(); } catch (Exception e) {} // note the {} - means "do nothing". I wanted it closed anyway. } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Concepts of Database Management

Authors: Philip J. Pratt, Mary Z. Last

8th edition

1285427106, 978-1285427102

More Books

Students also viewed these Databases questions