My objective is to only read 50 lines at a time from a text file, print them in reverse order and only store 50 lines a time in memory. In the most efficient way possible.

This is the code I have came up with but the output is not as expected. I have tested it out with an input file of 104 lines.

Actual Output: It prints line 50 to Line 1, Line 101 to Line 52 (skipping line 51), line 104 to 103 (skipping line 102). 

Expected Output: Line 50 - Line 1, Line 101 - Line 51, Line 104-102.

I also don't know how to change the first while loop so it keeps going until the end of the file, as testing out while (r.readLine != null) did not work either.

```
public static void doIt(BufferedReader r, PrintWriter w) throws IOException {
    Stack<String> s = new Stack<String>();
    int i = 0;
    int x = 0;

    while (x < 5) {
        for (String line = r.readLine(); line != null && i < 50; line = r.readLine()) {
            s.push(line);
            i++;
        }

        i = 0;

        while (!s.isEmpty()) {
            w.println(s.pop());

        }

        x++;

    }

}

```