CS Labs: Lab 12


To accompany Chapter 12 of An Introduction to Object-Oriented Programming with Java by C. Thomas Wu.

File Input and Output

There are 9 checkpoints in this lab. If you need help with any exercise, raise your hand.

Get the Lab 12 program files from the "read only" folder and put them in your Lab 12 directory.

Writing Our Own ls Command (sort of)

This section is not required, but I suggest you try it when you have finished the other parts

Change into subdirectory ListFiles and edit the ListFile class. Complete the program so that it behaves like the UNIX ls command. You will run your program by entering
java ListFile name of file or directory
If the name entered is a file name, it should simply be echoed at the console. If it is the name of a directory, all the file names and subdirectory names in that directory should be printed at the console. If there is no file or directory by that name, print an error message. For example, in this subdirectory, there are subdirectories A and B and the file ListFile.java. I should get these results:
         $ java ListFile A
         b
         c
         $ java ListFile ListFile.java
         ListFile.java
         $ java ListFile D            
         D: No such file or directory

Complete the code in the main() method of ListFile.java.

1 When you are ready, call us over to see your working program and code.

This section is not required, but I suggest you try it when you have finished the other parts

Now modify ListFile.java to use a JFileChooser object instead of getting the file name from the command line. The dialog window should open on the current directory, that is, the one in which ListFile.java is located. Check to see if the user has opened a file or cancelled. If the user has selected a file, print the file name at the console. If the user has cancelled, print "dialog cancelled" at the console.

2 When you are ready, call us over to see your working program and code.

Filtering out File Names

Notice that there is one more class in the ListFile directory, the OurFilter class. Make one last modification to ListFile.java. Add an OurFilter file filter to your JFileChooser so that only files ending in .txt will be displayed.

3 When you are ready, show us your modification.

Low-Level File I/O

Go into subdirectory LowLevelIO and edit the BytesInAndOut class. This program reads in an array of bytes from a file called message. The message is encrypted using a simple algorithm: 1 has been subtracted from the ASCI code of each byte. For example, if the message were Hello, in the message file you would find Gdkkn instead.

Complete this program so that the file message is decryted so that you can read the message. To do this, open the same file (that is message) for low-level output and write the byte array to it, but you need to do something to each byte before (or as) you write back to the file. What are you going to have to do to get the correct character back?

4 When you are ready, show us the finished program.

High-Level File I/0

Go into subdirectory HighLevelIO and edit the DataInAndOut class. Right now, this program opens a DataOutputStream to a file called data and writes out an int, a boolean, and a double. Run this program and examine the contents of the file data. Do you recognize the output? Why or why not?

Now uncomment the rest of the code in this file by taking out the line /* and also */. The code you uncommented, opens a DataInputStream to the file data. It reads an int, a boolean, and a double and writes them to the console using System.out.println(). Is the output what you expected? How does it compare to the contents of the data file?

5 When are ready with the answers to the above questions, call us over and explain the results of running this program.

Now edit the class TextInAndOut in this same subdirectory. This program writes the same data to file data as text strings, and then reads the data back in using a DataInputStream and prints the values to the console. Try to run this program. What is the problem? Notice that an exception is thrown. Fix the program so that it reads the same int, boolean, and double that were written to the text file. Hint: Use a BufferedReader instead of a DataInputStream and convert input lines to the appropriate data types.

6 When are ready, call us over to explain the problem with the program and show us your repair.

File I/O with Objects

Go into subdirectory ObjectIO and edit the MakeFile class. This program creates a file objects.dat and constructs and writes a random number of objects (between 1 and 1000) to the file. The objects written to the file are instances of one of three classes: Integer, Double, or Beans. The class that each object is an instance of is also randomly generated. Make sure you understand the code, and then compile and run it. There is a problem: an exception is thrown. What kind is it, and why was it thrown? How can you fix it if we still want three different classes of objects written to this file? (We just want objects from three different classes written to the file - we don't care which classes they are from.) Experiment with doing this.

7 When are ready, call us over to explain the problem with this program and how your fixed it so that when we run it, it creates a file with instances of three kinds of objects.

In the same directory, edit the class FilterIntegers. This program opens the file objects.dat (the file that was created using the MakeFile program) and it is supposed to read all the objects from the file. If an object is an "instanceof" the Integer class, it should be counted (use variable count) and printed at the console. Finish the code to do this in the try block. About how many Integers do you expect there to be, given the number of objects in the file?

8 When are ready, call us over to see your working FilterIntegers program. Be prepared to tell us about how many Integers you expect to find and why.

After the Lab

9 Show us that you have logged out, cleaned up, and pushed in your chairs for this last checkpoint.

End of Lab


Susan Haller and Timothy Fossum, University of Wisconsin-Parkside