4 Ways To Read Text Files In Java 8+ (without External Libraries)

Overview

Reading files is quite a common task in Java programming, and there are several ways to do so without relying on external libraries. In this tutorial, we will discuss four different ways to read text files in Java 8+ using built-in classes and APIs. We will go over examples of how to read text files using java.nio.file.Files, BufferedReader and FileReader, InputStream and FileInputStream, and Scanner. By the end of this tutorial, you will have a better understanding of the different ways to read files in Java and be able to choose the best approach for your specific use case.

In this post, we are going to use a single text file for demonstration purposes with the content as below:

First line
Second line

Read Files in Java using nio.Files

    public static void readUsingNIO(String filePath) throws IOException {
        Path path = Paths.get(filePath);

        //read all as a single string
        var content = new String(Files.readAllBytes(path));

        System.out.println(content);

        //read all as a list of strings, each string is a line
        var lines = Files.readAllLines(path);
        System.out.println(lines);
    }

Running the function above outputs the following result:

Reading files using NIO
Reading files using NIO

Read files using BufferedReader and FileReader

    public static void readUsingBufferedReader(String filePath) {
        try (BufferedReader br = new BufferedReader(new FileReader(filePath))) {
            String line;
            while ((line = br.readLine()) != null) {
                System.out.println(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

Running this will output:

Read files using BufferedReader and FileReader
Read files using BufferedReader and FileReader

Read files using FileInputStream

    public static void readUsingInputStream(String filePath) {
        try (FileInputStream fis = new FileInputStream(filePath)) {
            int b;
            while ((b = fis.read()) != -1) {
                System.out.print((char)b);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

Read file using Scanner

You can also use the Scanner class to read content from a file

    public static void readUsingScanner(String filePath) {
        try (Scanner scanner = new Scanner(new File(filePath))) {
            while (scanner.hasNextLine()) {
                System.out.println(scanner.nextLine());
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

    }

Pros and cons

While you can use any method above to read text files in java, there are some pros and cons of each method you need to take into account while using them:

MethodProsCons
nio.file.FilesConvenient, less code. Fast for small filesNot suitable for large files (memory intensive)
BufferedReaderLow memory footprint. Efficient even for large files
InputStreamDifficult to read specific parts of the file. Not suitable for reading text files
ScannerEfficient for small text fileNot suitable for large text file (similar to NIO)
Comparison of methods of reading text files in Java

Conclusion

In this tutorial, we have discussed four different ways to read files in Java 8+ using built-in classes and APIs.

Leave a Comment