Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

the java code below is supposed to read a raf file with 10 ints in it from back

ID: 3890499 • Letter: T

Question

the java code below is supposed to read a raf file with 10 ints in it from back to front. I keep getting a eofException every time i try to run it. where am i going wrong? any raf file of ints can be used.

import java.io.IOException;
import java.io.RandomAccessFile;

public class FileRead {

public static void main(String[] args) throws IOException{
int x;
RandomAccessFile file = new RandomAccessFile("c:\Users\jess\Desktop\data.raf","r");
for(int i = 0; i < (file.length()/4); i ++ ){
file.seek(file.length() - (i*4));
x = file.readInt();
System.out.println(x);
}
}
}

Explanation / Answer

import java.io.IOException;
import java.io.RandomAccessFile;

public class FileRead {
public static void main(String[] args) throws IOException{
int x;
RandomAccessFile file = new RandomAccessFile("c:\A1.raf","r");
for(int i = 1; i <= (file.length()/4); i ++ ){
file.seek(file.length() - (i*4));
x = file.readInt();
System.out.println(x);
}
}
}