import java.io.*; import java.util.*; public class FileViewer { private RandomAccessFile f; private Stack stk; private long topPos; ArrayList lines; private static final int SCREEN_SIZE = 24; public FileViewer(String fileName) throws IOException { f = new RandomAccessFile(fileName, "r"); stk = new Stack(); topPos = f.getFilePointer(); lines = new ArrayList(); readAndDisplay(); } public void next() throws IOException { stk.push(new Long(topPos)); topPos = f.getFilePointer(); readAndDisplay(); } public void previous() throws IOException { topPos = ((Long)stk.pop()).longValue(); f.seek(topPos); readAndDisplay(); } public void first() throws IOException { stk.clear(); topPos = 0; f.seek(topPos); readAndDisplay(); } public void last() throws IOException { do { stk.push(new Long(topPos)); topPos = f.getFilePointer(); } while (read()); display(); } public void close() throws IOException { f.close(); } boolean read() throws IOException { String line = null; lines.clear(); for (int i = 0; i < SCREEN_SIZE && (line = f.readLine()) != null; ++i) { lines.add(line); } return line != null; } void display() { for (int i = 0; i < lines.size() && i < SCREEN_SIZE; ++i) System.out.println((String) lines.get(i)); } void readAndDisplay() throws IOException { read(); display(); } } End of Listing