130k views
4 votes
Write an efficient program that reads the last 100 numbers in

the file (PrimeNumbers.dat). Don’t read all numbers from the file.
Skip all numbers before the last 100 numbers in the file. in
Java.

User AamirR
by
8.9k points

1 Answer

5 votes

Final answer:

To read the last 100 numbers in a file in Java, you can use the RandomAccessFile class. First, determine the size of the file and position the file pointer accordingly. Then, read the numbers using a loop.

Step-by-step explanation:

To read the last 100 numbers in a file in Java, you can use the RandomAccessFile class. First, determine the size of the file using the length() method. Then, use the seek() method to position the file pointer at the appropriate location, which is the file size minus 400 (100 numbers * 4 bytes each, assuming integers). Finally, read the numbers using the readInt() method in a loop, until you have read all 100 numbers.

User Paul Ardeleanu
by
8.2k points