8.2 Programming ReadMe - Write in Java and include comments The pledge of Allegi
ID: 3920265 • Letter: 8
Question
8.2 Programming ReadMe - Write in Java and include comments
The pledge of Allegiance states, “I pledge allegiance to the Flag of the United States of America, and to the Republic for which it stands: one Nation under God, indivisible, With Liberty and Justice for all.” Save the pledge, 174 characters, to a text file. Use the RandomAccessFile class to access the file. Using Seek method, display the characters at positions 124 and 135 only.
Directions
Create a text file with the pledge of allegiance named Pledge.txt using notepad.
Create a reference to the file, Pledge.txt, using the RandomAccessFile class.
Use the Seek method to point at positions 124 and 135, then display the characters represented by the byte streams.
Use try, catch and finally blocks for exception handling.
Grading Rubric
Task
Points
Throws clause added in main method of the ReadMe class
1
Create a reference of the RandomAccessFile class which points to Pledge.txt
1
Include try, catch and finally blocks for exception handling
1
Use the Seek method to point at positions 124 and 135, then display characters represented by the byte streams
1
Proper documentation
1
Program works effectively
1
Total
6
Task
Points
Throws clause added in main method of the ReadMe class
1
Create a reference of the RandomAccessFile class which points to Pledge.txt
1
Include try, catch and finally blocks for exception handling
1
Use the Seek method to point at positions 124 and 135, then display characters represented by the byte streams
1
Proper documentation
1
Program works effectively
1
Total
6
Explanation / Answer
ScreenShot
---------------------------------------------------
Program
/*This is a program Use the RandomAccessFile class to access the file.
*Using Seek method, display the characters at positions 124 and 135 only.
*/
//Packages for exception and RandomAccessFile class access
import java.io.IOException;
import java.io.RandomAccessFile;
public class ReadMe {
public static void main(String[] args)throws IOException {
//Try block has any IOException catch block generate error
try {
//RandomAccessFile object creation
RandomAccessFile raf = new RandomAccessFile("c:/Users/deept/Desktop/pledge.txt", "r");
//Seek character at position 124 of the file data
raf.seek(124);
//Create a stream to store read data
byte[] charAt124 = new byte[1];
//Stream to read file
raf.read(charAt124);
//Print result
System.out.println("Character at 124th position:"+new String(charAt124));
//Seek character at position 135 of the file data
raf.seek(135);
//Create a stream to store read data
byte[] charAt135 = new byte[1];
//Stream to read file
raf.read(charAt135);
//Print result
System.out.println("Character at 135th position:"+new String(charAt135));
//Close file access
raf.close();
}
//Exception catcher
catch(Exception e) {
e.printStackTrace();
}
//With or without exception finally block execute
finally {
System.out.println("File read exception handling added");
}
}
}
--------------------------------------------------------
Output
Character at 124th position:o
Character at 135th position:i
File read exception handling added
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.