Open In App

Different ways of Reading a text file in Java

Last Updated : 17 Nov, 2025
Comments
Improve
Suggest changes
57 Likes
Like
Report

In Java, there are multiple ways to read a text file depending on your data size and use case. The java.io and java.nio.file packages provide several classes to handle file reading efficiently. Let’s discuss the common approaches one by one.

1. Using the BufferedReader Class

BufferedReader class reads text from a character stream and buffers the characters for efficient reading. It is often wrapped around a FileReader or InputStreamReader to improve performance.

Syntax

BufferedReader in = new BufferedReader(Reader in, int size);

Java
import java.io.*;

public class UsingBufferReader {
    public static void main(String[] args) throws Exception{
        
        // Creating BufferedReader for Input
        BufferedReader bfri = new BufferedReader(
            new InputStreamReader(System.in));

        System.out.print("Enter the Path : ");

        // Reading File name
        String path = bfri.readLine();

        BufferedReader bfro
            = new BufferedReader(new FileReader(path));
        String st;

        while ((st = bfro.readLine()) != null)
            System.out.println(st);
    }
}


Output

UsingBufferReader
Output

2. FileReader class for Reading text file

The FileReader class is used to read text files in Java. It reads characters from a file and is suitable for reading plain text. The constructors of this class assume that the default character encoding and the default byte buffer size are appropriate. 

Constructors defined in this class are as follows:

  • FileReader(File file): Creates a new FileReader, given the File to read from
  • FileReader(FileDescriptor fd): Creates a new FileReader, given the FileDescriptor to read from
  • FileReader(String fileName): Creates a new FileReader, given the name of the file to read from
Java
import java.io.*;

public class UsingFileReader {

    public static void main(String[] args) throws Exception
    {

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        System.out.print("Enter the Path : ");
        
        // Reading File name
        String path = br.readLine();

        FileReader fr = new FileReader(path);

        int i;
      
        // Holds true till there is nothing to read
        while ((i = fr.read()) != -1)

            // Print all the content of a file
            System.out.print((char)i);
    }
}


Output

UsingFileReader
Output

3. Scanner class for reading text file

Scanner class provides a simple way to read text files and parse primitive types or strings using regular expressions. It splits the input into tokens using a delimiter (by default, whitespace).

Example 1: With using loops

Java
import java.io.*;
import java.util.Scanner;

public class UsingScannerClass 
{
    public static void main(String[] args) throws Exception
    {
      	BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        System.out.print("Enter the Path : ");
        
        // Reading File name
        String path = br.readLine();
      
        // pass the path to the file as a parameter
        File file = new File(path);
      
        Scanner sc = new Scanner(file);

        while (sc.hasNextLine())
            System.out.println(sc.nextLine());
    }
}


Output

UsingBufferReader
Output

Example 2: Without using loops

Java
import java.io.*;
import java.util.Scanner;

public class ReadingEntireFileWithoutLoop {
    public static void main(String[] args)
        throws IOException
    {
      	BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        System.out.print("Enter the Path : ");
        
        // Reading File name
        String path = br.readLine();
      
        File file = new File(path);
      
        Scanner sc = new Scanner(file);

        // we just need to use \\Z as delimiter
        sc.useDelimiter("\\Z");

        System.out.println(sc.next());
    }
}


Output

ReadingEntireFileWithoutLoop
Output

4. Reading the whole file in a List

We can read an entire text file into a List using the Files.readAllLines() method from the java.nio.file package. Each line in the file becomes one element in the list.

Syntax

public static List readAllLines(Path path,Charset cs)throws IOException

This method recognizes the following as line terminators: 

  • \u000D\u000A -> Carriage Return + Line Feed
  • \u000A -> Line Feed
  • \u000D -> Carriage Return
Java
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.*;
import java.util.*;

public class ReadFileIntoList 
{
    public static List<String> readFileInList(String fileName)
    {
      	// Created List of String
        List<String> lines = Collections.emptyList();
        
      	try {
            lines = Files.readAllLines(
                Paths.get(fileName),
                StandardCharsets.UTF_8);
        } catch(IOException e) {
            e.printStackTrace();
        }
      
        return lines;
    }
  
    public static void main(String[] args)
      throws IOException
    {
      
      	BufferedReader br = new
          BufferedReader(new InputStreamReader(System.in));

        System.out.print("Enter the Path : ");
        
        // Reading File name
        String path = br.readLine();
      
        List l = readFileInList(path);
	
      	// Iterator iterating over List
        Iterator<String> itr = l.iterator();
      
        while (itr.hasNext())
            System.out.println(itr.next());
    }
}


Output

ReadFileIntoList
Output

5. Read a text file as String

We can read an entire text file as a single String in Java. This is useful when you want to process the file content as a whole rather than line by line.

Syntax:

String data = new String(Files.readAllBytes(Paths.get(fileName)));

Example:

Java
package io;

import java.nio.file.*;

public class ReadTextAsString {

    public static String readFileAsString(String fileName)
        throws Exception
    {
        String data = "";
        data = new String(
            Files.readAllBytes(Paths.get(fileName)));
        return data;
    }

    public static void main(String[] args) throws Exception
    {
      	BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        System.out.print("Enter the Path : ");
        
        // Reading File name
        String path = br.readLine();
      
        String data = readFileAsString(path);
      
        System.out.println(data);
    }
}

Output

ReadTextAsString
Output

Reading and Writing in a File
Visit Course explore course icon
Article Tags :

Explore