Question: 1. Run ReadRandomLetters.java using Letters.dat 2. Move to character 100, and use EOFException to catch the error 3. Change the file to read and write

1. Run ReadRandomLetters.java using Letters.dat
2. Move to character 100, and use EOFException to catch the error
3. Change the file to read and write using "rw
4. Printout the last char in the file (should be z)
5. Print out the middle characters in the file (should be m + n)
6. Write at the end of the file the characters A, B, Z (uppercase)
7. Put the string I am in the middle between the lowercase characters and the uppercase characters
8. Find the length of the file in bytes
9. Printout all the characters in file
ReadRandomLetters.java
import java.io.*;
/**
This program uses the RandomAccessFile class to open
the file Letters.dat and randomly read letters from
different locations.
*/
public class ReadRandomLetters
{
public static void main(String[] args) throws IOException
{
final int CHAR_SIZE = 2;// 2 byte characters
long byteNum; // The byte number
char ch; // A character from the file
// Open the file for reading.
RandomAccessFile randomFile =
new RandomAccessFile("Letters.dat", "r");
// Move to the character 5. This is the 6th
// character from the beginning of the file.
byteNum = CHAR_SIZE * 5;
randomFile.seek(byteNum);
// Read the character stored at this location
// and display it. Should be the letter f.
ch = randomFile.readChar();
System.out.println(ch);
// Move to character 10 (the 11th character),
// read the character, and display it.
// Should be the letter k.
byteNum = CHAR_SIZE * 10;
randomFile.seek(byteNum);
ch = randomFile.readChar();
System.out.println(ch);
// Move to character 3 (the 4th character),
// read the character, and display it.
// Should be the letter d.
byteNum = CHAR_SIZE * 3;
randomFile.seek(byteNum);
ch = randomFile.readChar();
System.out.println(ch);
// Close the file.
randomFile.close();
}
}
Letters.dat
a b c d e f g h i j k l m n o p q r s t u v w x y z

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!