Read Text Files in Java

Tags: Java File Java IO Java NIO

Introduction

In this tutorial we are going to explore how to read text files in a Java program. We provide different solutions to show how to use different Java core classes of Java IO and NIO packages to read files.

Java NIO Files.readAllLines() static method

In this Java code example we use Files.readAllLines() method to read all lines from a file into a list of String.

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

public class FilesReadAllLinesExample {
    public static void main(String... args) {
        try {
            String fileName = "test.txt";
            Path filePath = Paths.get(fileName);

            // Read all lines of a file into a list of String.
            List<String> lines = Files.readAllLines(filePath);

            for(String line : lines) {
                System.out.println(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Java NIO Files.lines() static method

In this Java code example we use Files.lines() method to read all lines from a file into a Stream.

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.stream.Stream;

public class FilesLinesExample {
    public static void main(String... args) {
        try {
            String fileName = "test.txt";
            Path filePath = Paths.get(fileName);

            // Read all lines from a file into a Stream
            Stream<String> stream = Files.lines(filePath);

            // print to system output
            stream.forEach(System.out::println);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Java NIO Files.readAllBytes() static method

In this Java code example we use Files.readAllBytes() method to read all bytes of a file and convert it into a String.

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class FilesReadAllBytesExample {
    public static void main(String... args) {
        try {
            String fileName = "test.txt";
            Path filePath = Paths.get(fileName);

            // Read all bytes in a file
            byte[] fileBytes = Files.readAllBytes(filePath);
            
            // Convert bytes into String
            String fileContent = new String(fileBytes);

            System.out.println(fileContent);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Java NIO Files.newInputStream() static method

In this Java code example we use Files.newInputStream() method to create an input stream to read content from a file.

import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class FilesNewInputStreamExample {
    public static void main(String... args) {
        try {
            String fileName = "test.txt";
            Path filePath = Paths.get(fileName);

            // Create new input stream to read data from a file
            InputStream inputStream = Files.newInputStream(filePath);
            
            int readChar;
            while((readChar = inputStream.read()) != -1) {
                System.out.print((char)readChar);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Java IO FileReader

In this Java code example we use FileReader class from the Java IO package to read file content.

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class FileReaderReadExample {
    public static void main(String... args) {
        String fileName = "test.txt";

        try(FileReader fileReader = new FileReader(fileName)) {

            int readChar;
            while((readChar = fileReader.read()) != -1) {
                System.out.print((char)readChar);
            }
            
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Java IO FileReader and BufferedReader

The following Java code examples we use BufferedReader to read the text file line by line.

Using BufferedReader.readLine() method.

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

public class BufferedReaderReadLineExample {
    public static void main(String... args) {
        String fileName = "test.txt";
        try(FileReader fileReader = new FileReader(fileName);
            BufferedReader bufferedReader = new BufferedReader(fileReader)) {

            String lineOfText = null;
            while ((lineOfText = bufferedReader.readLine()) != null) {
                System.out.println(lineOfText);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Using BufferedReader.lines() method.

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

public class BufferedReaderLinesExample {
    public static void main(String... args) {
        String fileName = "test.txt";
        try(FileReader fileReader = new FileReader(fileName);
            BufferedReader bufferedReader = new BufferedReader(fileReader)) {

            bufferedReader.lines().forEach(System.out::println);

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Java IO FileInputStream

In this Java code example we use FileInputStream to read one byte at a time from file content.

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class FileInputStreamExample {
    public static void main(String... args) {
        String fileName = "test.txt";
        File file = new File(fileName);

        try(FileInputStream fileInputStream = new FileInputStream(file)) {
            int readChar;

            while((readChar = fileInputStream.read()) != -1) {
                System.out.print((char)readChar);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Java IO FileInputStream and BufferedInputStream

In this Java code example we use BufferedInputStream class to read one byte at a time from file content.

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class BufferedInputStreamReadExample {
    public static void main(String... args) {
        String fileName = "test.txt";
        File file = new File(fileName);

        try(FileInputStream fileInputStream = new FileInputStream(file);
            BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream)) {

            int readChar;
            while((readChar = bufferedInputStream.read()) != -1) {
                System.out.print((char)readChar);
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Java IO FileInputStream and InputStreamReader

In this Java code example we use InputStreamReader class to read one byte at a time from file content.

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;

public class InputStreamReaderExample {
    public static void main(String... args) {
        String fileName = "test.txt";
        File file = new File(fileName);

        try(FileInputStream fileInputStream = new FileInputStream(file);
            InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream)) {

            int readByte;
            while((readByte = inputStreamReader.read()) != -1) {
                System.out.print((char)readByte);
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Java IO RandomAccessFile

In the Java code example below we use RandomAccessFile class to read text file content line by line.

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

public class RandomAccessFileExample {
    public static void main(String... args) {
        String fileName = "test.txt";

        try(RandomAccessFile randomAccessFile = new RandomAccessFile(fileName, "r")) {

            String lineOfText;
            while((lineOfText = randomAccessFile.readLine()) != null) {
                System.out.println(lineOfText);
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Java Util Scanner

The Java Util package also provides the Scanner class that can be used to read text file line by line.

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

public class ScannerExample {
    public static void main(String... args) {
        String fileName = "test.txt";
        File file = new File(fileName);

        try(Scanner scanner = new Scanner(file)) {

            while(scanner.hasNext()) {
                String lineOfText = scanner.nextLine();

                System.out.println(lineOfText);
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
}

Happy Coding 😊

Write Text to a File in Java