You can't read the whole file in memory and process it line by line. The memory size is a constraint for the computer and even for servers. So, only one method is possible and it is of reading the file line by line.
In Java you can use the BufferedReader class for reading the file line by line. Reading file one line at a time using the BufferedReader class uses very less memory and the program works very fast.
Check the tutorial: Java Read File Line by Line.
The Scanner class can also be used for reading the file line by line. Check this tutorial: Line by Line reading from a file using Scanner Class.