CS 178 Programming with Multimedia Objects

Fall 2004

Recitation Exercise:

Prepared by: Aditya Mathur.

To be completed in class on Oct 29, 2004.

Learning objectives:
  1. How to set up file I/O and data I/O streams and do input/output.
  2. How to handle exceptions.
  3. Reference: Chapters 9.1, 9.2, and 10.2.

Complete the following set of instructions to develop a few simple methods. Be careful about where in the program you declare a variable. The try creates a block and so does catch. Any variables declared inside the try and catch blocks are local to their respective blocks.

Global Array and other declarations:

  1. Declare maxGenes as an integer constant and initialize it to 100.
  2. Declare maxNameLen as an integer constant and initialize it to 20.
  3. Declare errorField as TextField with 50 characters and gnameDisp as a TextArea of 20 rows and 25 columns.

Write the following two methods.

public DataInputStream fileOpen(String fileName)

  1. Declare inFile as a FileInputStream that opens a file whose name is passed to it as a parameter String filename. Display the following message in TextField errorField if any exception is generated: "File filename could not be opened. xxxx" where xxxx is the message returned by Java runtime and filename should be replaced by the name of the file supplied to the fileOpen method.
  2. Declare inDataFile as a DataInputStream that connects to inFile. "File filename could not be opened. xxxx" where xxxx is the message returned by Java runtime and filename should be replaced by the name of the file supplied to the fileOpen method.
  3. If no exceptions are generated then return inDataFile.

public void readAndDisplayGenes(DataInputStream inStream)

  • This method reads data from the inStream data stream and displays the gene names in gnameDisp.
  • The data is to be read as follows:
    • Declare geneName as an array of characters with maxNameLen elements.
    • Read an integer that denotes the number of genes in the file.
    • Next read an integer that denotes the length of the first gene name.
    • Next read the characters in the gene name. Save these characters in a char array named geneName.
    • Next convert the characters in the char array geneName to a string gName using the following statement:
    • String gname=new String(geneName);

    • Display gname in the gnameDisp area.
    • Read and display the remaining gene names as described above.
    • Display the following message in TextField errorField if any exception is generated while reading data from the file: "Error while reading file. xxxx" where xxxx is the message returned by Java runtime.

As an example, suppose that the file genes contains the following data:

2 9 cleaopatra 5 tudor

Your method should read two gene names in the file and display them in two text fields.

As another example, the following file should lead to an exception while reading data:

2 9 cleaopat 5 tudor

Last update: October 29, 2004

Solution (source)

This solution is an applet and contains several methods that you were not asked to write during the recitation.

This will work as desired only if compiled and executed on the same computer.