|
|
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
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.
Question 2. What class in the
java.util.zippackage gives you access to the entries in a ZIP archive and allows you to read those entries through a stream?
Answer 2.ZipInputStream. 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 theFileWriterand
BufferedWriterclasses to append data to the end of the text file. Here is the
FileWriterconstructor, you pass intrueto write to the file in append mode:An alternate answer is to useFileWriter writer = new FileWriter (String filename, boolean append);RandomAccessFileand 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 thejava.util.StringTokenizeror
java.io.StreamTokenizerto 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.
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 aint 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); }RandomAccessFile). In this exercise, you should add two buffers: theInputStreamis wrapped within a
BufferedInputStreamand the
InputStreamReaderis wrapped within a
BufferedReader. 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); }
Exercise 1. Modify the program discussed in the section How to Use Pipe Streams
so that it uses input streams and output streams in place of readers and writers.
Answer 1. SeeRhymingWords.java.
Exercise 2. Implement a pair of classes, one
Readerand oneWriter, that count the number of times a particular character, such ase, is read or written. The character can be specified when the stream is created. Write a program to test your classes. You can usefarrago.txtas the input file.
Answer 2. See the following three files:CounterDemo.java,
CountReader.java, and
CountWriter.java.
Exercise 3. The file
datafilebegins with a single
longthat tells you the offset of a single int piece of data within the same file. Using theRandomAccessFileclass, write a program that gets the int piece of data. What is theintdata?
Answer 3.123. SeeFileReader.javafor the solution. If you're interested in seeing how the file was written, see
FileWriter.java.
Exercise 4. In this exercise, you'll implement object serialization for the
Card2class.
Exercise 4a. Rename the class
Card3and make it serializable.
Answer 4a. SeeCard3.java.
Exercise 4b. Create a program named
CardWriterthat creates aCard3instance, displays its value, and serializes it into a file named card.out. Here is an example of whatCardWritermight display:
Card to write is: Ace of SpadesAnswer 4b. See
CardWriter.java.
Exercise 4c. Create a program named
CardReaderthat reads theCard3object from card.out and displays its value. For example:
Card read is: Ace of SpadesAnswer 4c. See
CardReader.java.
|
|
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
Copyright 1995-2004 Sun Microsystems, Inc. All rights reserved.