Writing Student Info in a Randomaccessfile in Java
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
.Question 2. What class in the
java.util.zip
package 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 theFileWriter
andBufferedWriter
classes to append data to the end of the text file. Here is theFileWriter
constructor, you pass intrue
to write to the file in append mode:An alternate answer is to useFileWriter writer = new FileWriter (String filename, boolean append);RandomAccessFile
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 thejava.util.StringTokenizer
orjava.io.StreamTokenizer
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.
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: theInputStream
is wrapped within aBufferedInputStream
and theInputStreamReader
is wrapped within aBufferedReader
. 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 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
Reader
and 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.txt
as the input file.
Answer 2. See the following three files:CounterDemo.java
,CountReader.java
, andCountWriter.java
.Exercise 3. The file
datafile
begins with a singlelong
that tells you the offset of a single int piece of data within the same file. Using theRandomAccessFile
class, write a program that gets the int piece of data. What is theint
data?
Answer 3.123
. SeeFileReader.java
for the solution. If you're interested in seeing how the file was written, seeFileWriter.java
.Exercise 4. In this exercise, you'll implement object serialization for the
Card2
class.Exercise 4a. Rename the class
Card3
and make it serializable.
Answer 4a. SeeCard3.java
.Exercise 4b. Create a program named
CardWriter
that creates aCard3
instance, displays its value, and serializes it into a file named card.out. Here is an example of whatCardWriter
might display:
Card to write is: Ace of Spades
Answer 4b. See
CardWriter.java
.Exercise 4c. Create a program named
CardReader
that reads theCard3
object from card.out and displays its value. For example:
Card read is: Ace of Spades
Answer 4c. See
CardReader.java
.
Start of Tutorial > Start of Trail > Start of Lesson | Search Feedback Form |
Copyright 1995-2005 Sun Microsystems, Inc. All rights reserved.
Writing Student Info in a Randomaccessfile in Java
Source: http://www.iitk.ac.in/esc101/05Aug/tutorial/essential/io/QandE/answers.html
0 Response to "Writing Student Info in a Randomaccessfile in Java"
Post a Comment