Question: Write a program similar to the one in Listing 10.10 that can write an arbitrary number of Species objects to a binary file. (Species appears
Write a program similar to the one in Listing 10.10 that can write an arbitrary number of Species objects to a binary file. (Species appears in Listing 5.19 of Chapter 5.) Read the file name and the data for the objects from a text file that you create by using a text editor. Then write another program that can search a binary file created by your first program and show the user the data for any requested endangered species. The user gives the file name and then enters the name of the species. The program either displays all the data for that species or gives a message if that species is not in the file. Allow the user to either enter additional species’ names or quit.
Listing 10.10
![import java.io.FileInputStream; import java.io.FileOutputStream; i mport java.io.I0Exception; import java.io.ObjectInputStream; i mport java.io.0bject0utputStream; public class ClassObjectI0Demo { public static void main(String[]args) { ObjectoutputStream outputStream = null; String fileName =](https://dsd5zvtm8ll6.cloudfront.net/si.question.images/images/question_images/1606/3/7/2/1405fbf4b2c753171606372139172.jpg)

Listing 5.19
![public class ClassObjectI0Demo { public static void main(String[]args) { ObjectoutputStream outputStream =](https://dsd5zvtm8ll6.cloudfront.net/si.question.images/images/question_images/1606/3/7/2/2045fbf4b6ca9a671606372203635.jpg)
import java.io.FileInputStream; import java.io.FileOutputStream; i mport java.io.I0Exception; import java.io.ObjectInputStream; i mport java.io.0bject0utputStream; public class ClassObjectI0Demo { public static void main(String[]args) { ObjectoutputStream outputStream = null; String fileName = "species.records"; try { outputStream = new ObjectOutputStream( new FileOutputStream (fileName)); } catch (I0Exception e) { System.out.println("Error opening output file " fileName + "."); System.exit(0); Species cali fCondor = new Species ("Calif. Condor", 27, 0.02); Species blackRhino = new Species ("Black Rhino", 100, 1.0); try { outputStream.writeObject (califCondor); outputStream. writeObject (blackRhino); outputStream.Close(); catch (10Exception e) { System.out.println("Error wring to file " + fileName + "."); System. exit (0) } System.out.println("Records sent to file fileName + "."); System.out.println( "Now let's reopen the file and echo "the records."):
Step by Step Solution
3.37 Rating (156 Votes )
There are 3 Steps involved in it
public class WriteSpeciesFile public static void mainString args ObjectOutputStream outputStream null ObjectInputStream inputStream null String fileName getFileNameEnter output file name try outputStr... View full answer
Get step-by-step solutions from verified subject matter experts
