### How to read a text file in Java? In short, Scanner. ### Demo code Code below showing how to use `Scanner` class to read a text file line by line in Java. ```java try { // init file File mFile = new File("/root/example.txt"); // init scanner Scanner mScanner = new Scanner(mFile); // while has next line while( mScanner.hasNextLine() ) { // get line string String mLine = mScanner.nextLine(); // do something } } catch ( FileNotFoundException e ) { // trace error e.printStackTrace() } ```