Skip to content

Java read file line by line | Text File | Scanner | Examples

  • by

Sometimes we need to Java read file line by line. For example, the text file has data of student roll number and name line by line. So how you will do it? There are many ways to do it in java. In this tutorial, you will learn about Reading file line by line in Java using the scanner class and more ways with examples.

Java read file line by line Text File Scanner Examples

If you are looking only for reading a file then follow this tutorial link – Java Read File Text

Ways to do Java read file line by line

  • BufferedReader
  • Scanner class
  • Using Files
  • RandomAccessFile

Let’s start with Example 

We will see Java read the text file line by line example one by one different method.

The first example, you can look a screenshot of project structure. And Output will be the same for all examples because using the same file.

Java read file line by line example

Read Line using BufferedReader

In BufferedReader use readLine() method to read file line by line to String.  readLine() method returns null at the end of the file. See the below simple example of how to read file line by line with BufferedReader.

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class FileReading {

    public static void main(String[] args) {

        try {
            BufferedReader reader = new BufferedReader(new FileReader(
                    "samplefile.txt"));
            String line = reader.readLine();

            while (line != null) {
                System.out.println(line);
                // read next line
                line = reader.readLine();
            }
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Output: First Line
Second Line

Scanner

Here how java read the file line by line scanner example. Open the file and print the content line by line.

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class FileReading {

    public static void main(String[] args) {

        try {
            Scanner scanner = new Scanner(new File("samplefile.txt"));
            while (scanner.hasNextLine()) {
                System.out.println(scanner.nextLine());
            }
            scanner.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
}

Using Files Class

Files class is a utility class that contains various useful methods. A readAllLines method used to read all the file lines into a list of strings.

In an example, it’s a return a list so you need to run a for loop to get the line by line text. See below java read lines from text file example. In the output, you can see the last line is a list.

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;


public class FileReading {

    public static void main(String[] args) {
        try {

            List<String> allLines = Files.readAllLines(Paths.get("samplefile.txt"));
            for (String line : allLines) {
                System.out.println(line);
            }
            System.out.println("Read line list " + allLines);
        } catch (IOException e) {
            e.printStackTrace();

        }
    }
}

Output: 1 Line – Java read
2 Line – List data
Read line list [1 Line – Java read, 2 Line – List data]

RandomAccessFile

Use a RandomAccessFile read mode and readLine method to read file line by line.

import java.io.IOException;
import java.io.RandomAccessFile;


public class FileReading {

    public static void main(String[] args) {
        try {
            RandomAccessFile file = new RandomAccessFile("samplefile.txt", "r");
            String str;
            while ((str = file.readLine()) != null) {
                System.out.println(str);
            }
            file.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Question: How to Java Read Text File line by line into Array?

Answer: Using Files Class methods to read text line by line will be a very easy way to do it. Just convert the list to Array as below code.

String[] arrayLines = allLines.toArray(new String[0]);

Complete code

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;


public class FileReading {

    public static void main(String[] args) {
        try {

            List<String> allLines = Files.readAllLines(Paths.get("samplefile.txt"));
            String[] arrayLines = allLines.toArray(new String[0]);
            System.out.println("Array " + arrayLines);
        } catch (IOException e) {
            e.printStackTrace();

        }
    }
}

Output: Array [Ljava.lang.String;@3d24753a

Do comment in below if any doubt and suggestions or examples.

Note: This example (Project) is developed in IntelliJ IDEA 2018.2.6 (Community Edition)
JRE: 11.0.1
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
macOS 10.14.1

Java version 11

All Java read file line by line are in Java 11, so it may change on different from Java 9 or 10 or upgraded versions.|

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading