How do I read file line by line using java.util.Scanner class?

Here is a compact way to read file line by line using the java.util.Scanner class.

package org.kodejava.example.util;

import java.util.Scanner;

public class ScannerReadFile {
    public static void main(String[] args) {
        // Create an instance of File for data.txt file.
        File file = new File("data.txt");
        try {
            // Create a new Scanner object which will read the data
            // from the file passed in. To check if there are more 
            // line to read from it we check by calling the 
            // scanner.hasNextLine() method. We then read line one 
            // by one till all lines is read.
            Scanner scanner = new Scanner(file);
            while (scanner.hasNextLine()) {
                String line = scanner.nextLine();
        } catch (FileNotFoundException e) {

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.