In this blog post, we will cover the various methods to read a plain text file in Java. Java provides multiple ways to read text from a file, each having its own advantages and disadvantages. By the end of this post, you will have a good understanding of how to read a plain text file in Java and be able to choose the best method for your needs.

1. Using the BufferedReader Class

The BufferedReader class is the simplest way to read a plain text file in Java. It provides a readLine() method to read a single line of text from the file at a time. Here is an example of how to use the BufferedReader class to read a file:

import java.io.*;

public class Main {
  public static void main(String[] args) {
    try {
      // Open the file
      File file = new File("file.txt");
      BufferedReader br = new BufferedReader(new FileReader(file));

      // Read each line of the file
      String line;
      while ((line = br.readLine()) != null) {
        System.out.println(line);
      }

      // Close the file
      br.close();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}

2. Using the Scanner Class

The Scanner class is another way to read a plain text file in Java. It provides a more flexible way of reading text from a file, allowing you to read data of different types, such as integers and doubles, in addition to strings. Here is an example of how to use the Scanner class to read a file:

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

public class Main {
  public static void main(String[] args) {
    try {
      // Open the file
      File file = new File("file.txt");
      Scanner sc = new Scanner(file);

      // Read each line of the file
      while (sc.hasNextLine()) {
        System.out.println(sc.nextLine());
      }

      // Close the file
      sc.close();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}

3. Using the Files Class (Java 7+)

The Files class was introduced in Java 7 and provides a more modern and efficient way to read a plain text file in Java. The Files class provides a readAllLines() method that can be used to read all the lines of a file into a List<String>. Here is an example of how to use the Files class to read a file:

import java.io.*;
import java.nio.file.*;
import java.util.List;

public class Main {
  public static void main(String[] args) {
    try {
      // Open the file
      Path path = Paths.get("file.txt");
      List lines = Files.readAllLines(path);

      // Read each line of the file
      for (String line : lines) {
        System.out.println(line);
      }
   

4. Using the FileInputStream Class

The FileInputStream class provides a low-level way of reading binary data from a file, including plain text files. This method is useful when you need to read a file in a more fine-grained manner, but it is not as convenient as the other methods described above. Here is an example of how to use the FileInputStream class to read a file:

import java.io.*;

public class Main {
  public static void main(String[] args) {
    try {
      // Open the file
      File file = new File("file.txt");
      FileInputStream fis = new FileInputStream(file);

      // Read the contents of the file
      int content;
      while ((content = fis.read()) != -1) {
        System.out.print((char) content);
      }

      // Close the file
      fis.close();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}

Conclusion

In this blog post, we have covered the various methods to read a plain text file in Java. Whether you need to read a file line by line or read all its contents at once, Java provides multiple options for you to choose from. By using the BufferedReader, Scanner, Files, or FileInputStream class, you can read a plain text file in Java with ease.

 

Leave a Reply