Prepare each line you read for a line and print the line. If you run out of lines for reading, you simply print what you have.
Alternatively, if you are sure about the number of lines you have and you do not want to use the line:
void printReversed(int n, BufferedReader reader)
{
LineNumberReader lineReader = new LineNumberReader(reader);
while (--i >= 0)
{
lineReader.setLineNumber(i);
System.out.println(lineReader.readLine());
}
}
source
share