B.Tech Students: Apply for Live Programming Internship C, C++, Java, Python ,Web page Designing, PHP Java File Reading | Login Technologies

Java File Reading: Learn Different Methods

Reading files in Java is a common task—whether you're loading configuration settings, reading saved user data, or processing logs. Java offers multiple ways to do this effectively. Here we'll learn how to read files using BufferedReader, Scanner, and FileInputStream with clear examples and explanations.


1. Reading with BufferedReader (Recommended)

BufferedReader is the most common way to read text files in Java. It reads text efficiently, line by line, making it ideal for logs and configuration files.

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

public class ReadFileBufferedReader {
  public static void main(String[] args) {
    try {
      FileReader fr = new FileReader("example.txt");
      BufferedReader br = new BufferedReader(fr);
      String line;
      while ((line = br.readLine()) != null) {
        System.out.println(line);
      }
      br.close();
      fr.close();
    } catch (IOException e) {
      System.out.println("Error reading file: " + e.getMessage());
    }
  }
}

Key Points:

  • Reads character streams.
  • Efficient for large text files.
  • Uses readLine() for easy line-by-line reading.
  • Always close readers to avoid resource leaks.

2. Reading with Scanner (Easy to Use)

Scanner is simple and beginner-friendly. It reads input line by line using hasNextLine() and nextLine().

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

public class ReadFileScanner {
  public static void main(String[] args) {
    try {
      File file = new File("example.txt");
      Scanner sc = new Scanner(file);
      while (sc.hasNextLine()) {
        System.out.println(sc.nextLine());
      }
      sc.close();
    } catch (FileNotFoundException e) {
      System.out.println("File not found!");
    }
  }
}

Advantages:

  • Very easy syntax for beginners.
  • Good for small text files or simple parsing.
  • Handles line-by-line input cleanly.

3. Reading with FileInputStream (Byte Stream)

FileInputStream reads files byte by byte. It's suitable for binary data or low-level processing.

import java.io.*;

public class ReadFileInputStream {
  public static void main(String[] args) {
    try {
      FileInputStream fis = new FileInputStream("example.txt");
      int ch;
      while ((ch = fis.read()) != -1) {
        System.out.print((char) ch);
      }
      fis.close();
    } catch (IOException e) {
      System.out.println("Error reading file");
    }
  }
}

Use Cases:

  • Reading binary files (images, audio, etc.).
  • Low-level control over file input.

Final Notes

  • Always use try-catch to handle exceptions when reading files in Java.
  • Close your streams or readers to free resources and avoid memory leaks.
  • Use BufferedReader for large text files, Scanner for simplicity, and FileInputStream for binary data.