The JavaTM Tutorial
Previous Page Lesson Contents Start of Tutorial > Start of Trail > Start of Lesson Search
Feedback Form

Trail: Essential Java Classes
Lesson: I/O: Reading and Writing (but no 'rithmetic)

Answers to Questions and Exercises: I/O: Reading and Writing (but no 'rithmetic)

Questions

Question 1. What class would you use to read a few pieces of data that are at known positions near the end of a large file?
Answer 1. RandomAccessFile(in the API reference documentation).

Question 2. What class in the java.util.zip(in the API reference documentation) package gives you access to the entries in a ZIP archive and allows you to read those entries through a stream?
Answer 2. ZipInputStream(in the API reference documentation). This class implements an input stream filter for reading files in the ZIP file format.

Question 3. How would you append data to the end of a file? Show the constructor for the class you would use and explain your answer.
Answer 3. Here's a quick answer: Use the FileWriter(in the API reference documentation) and BufferedWriter(in the API reference documentation) classes to append data to the end of the text file. Here is the FileWriter constructor, you pass in true to write to the file in append mode:

FileWriter writer = new FileWriter (String filename, boolean append);
An alternate answer is to use RandomAccessFile(in the API reference documentation) and skip to the end of the file and start writing:

...
RandomAccessFile file = new RandomAccessFile(datafile, "rw");
file.skipBytes((int)file.length()); //skip to the end of the file
file.writeBytes("Add this text to the end of datafile"); //write at the end of the file
file.close();
...

Question 4. Suppose you wanted to write code that reads from a file one word at a time. The code needs to peek ahead to find where the words are separated by whitespace. What input stream could you use to accomplish this?
Answer 4. Answer 4: You can use the java.util.StringTokenizer(in the API reference documentation) or java.io.StreamTokenizer(in the API reference documentation) to parse your input into words. Each class has a default set of delimiters (like white space) that you can specify.

Question 5. How can you improve the performance of the following code? Explain your answer and show the new line(s) of code.

int i;
URL url = new URL("http://java.sun.com/");
URLConnection javaSite = url.openConnection();
InputStream input = javaSite.getInputStream();
InputStreamReader reader = new InputStreamReader(input);
while ((i = reader.read()) != -1) {
    System.out.print(i);
}
Answer 5. Buffer! To improve the performance of your code, you should buffer all your input and output where possible (remember you can't buffer a RandomAccessFile). In this exercise, you should add two buffers: the InputStream(in the API reference documentation) is wrapped within a BufferedInputStream(in the API reference documentation) and the InputStreamReader(in the API reference documentation) is wrapped within a BufferedReader(in the API reference documentation). Here is the revised code (changes shown in bold):

int i;
URL url = new URL("http://java.sun.com/");
URLConnection javaSite = url.openConnection();
InputStream input = javaSite.getInputStream();
BufferedInputStream in = new BufferedInputStream(input);
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
while ((i = reader.read()) != -1) {
    System.out.print(i);
}

Exercises

Exercise 1. Modify the program discussed in the section How to Use Pipe Streams(in the Essential Java Classes trail) so that it uses input streams and output streams in place of readers and writers.
Answer 1. See RhymingWords.java(in a .java source file).

Exercise 2. Implement a pair of classes, one Reader and one Writer, that count the number of times a particular character, such as e, is read or written. The character can be specified when the stream is created. Write a program to test your classes. You can use farrago.txt(in a .java source file) as the input file.
Answer 2. See the following three files: CounterDemo.java(in a .java source file), CountReader.java(in a .java source file), and CountWriter.java(in a .java source file).

Exercise 3. The file datafile(in a .java source file) begins with a single long that tells you the offset of a single int piece of data within the same file. Using the RandomAccessFile class, write a program that gets the int piece of data. What is the int data?
Answer 3. 123. See FileReader.java(in a .java source file) for the solution. If you're interested in seeing how the file was written, see FileWriter.java(in a .java source file).

Exercise 4. In this exercise, you'll implement object serialization for the Card2(in a .java source file) class.

Exercise 4a. Rename the class Card3 and make it serializable.
Answer 4a. See Card3.java(in a .java source file).

Exercise 4b. Create a program named CardWriter that creates a Card3 instance, displays its value, and serializes it into a file named card.out. Here is an example of what CardWriter might display:

      Card to write is: Ace of Spades

Answer 4b. See CardWriter.java(in a .java source file).

Exercise 4c. Create a program named CardReader that reads the Card3 object from card.out and displays its value. For example:

      Card read is: Ace of Spades

Answer 4c. See CardReader.java(in a .java source file).



Previous Page Lesson Contents Start of Tutorial > Start of Trail > Start of Lesson Search
Feedback Form

Copyright 1995-2002 Sun Microsystems, Inc. All rights reserved.