Reads the lines (Strings) of a text-file, one line at a time, until the end-of-file.
BufferedReader file
= new BufferedReader(new FileReader(filename));
Opens a text-file for reading.
while (file.ready())
Tests whether the file is ready for reading - the fails at the end-of-file,
so the loop will stop there.
try...
catch (IOException e)
Handles possible IOException errors, such as File-not-found.
import java.io.*;
All file classes and methods are contained in java.io.*.
Text-file IO is straightforward, similar to other languages.
The program must handle the IOExceptions - Java requires this. An alternative is to "throw" the errors, but in longer programs this results in a long chain of throws, up to the top level of the program. Try..Catch is probably simpler (and better).
The practice of simply displaying Java's standard Exception errors is not very user friendly - real programs should handle errors more productively.
Some care may be required in some operating systems, as the "\n" does not use the same byte-codes in all OS's. That means a file written in one OS might not be readable in another. For example:
Linux : \n = (char)10
Windows: \n = (char)13 + (char)10