File Handling in Java

file handling in java

File handling in Java allows developers to perform operations such as creating, reading, writing, and deleting files. Java provides the java.io package for handling basic file operations and the java.nio.file package for advanced and efficient file handling.

Use cases include:

  • Logging system activities.
  • Saving user data or application configurations.
  • Processing large datasets stored in files.

In this step by step guide, we will cover the fundamentals of file handling and then go deeper into the more advanced operations with practical examples so that you understand this topic well. Let’s get started!

File Class: The Basics

The File class is the backbone of Java file handling, representing the path to a file or directory. This class does not directly read or write data but helps manage file metadata and operations like creation, deletion, and inspection.

Suggested Read: OOPs Concepts in Java with Examples

Key Methods:

  1. createNewFile(): Creates a new file.
  2. exists(): Checks if the file exists.
  3. delete(): Deletes the file.
  4. getName(), getPath(): Retrieve file name or path.

1. Creating a File

Code Example:

import java.io.File;

public class FileExample {
    public static void main(String[] args) {
        try {
            // Create a File object with the specified path
            File file = new File("example.txt");

            // Check if the file does not exist, then create it
            if (file.createNewFile()) {
                System.out.println("File created: " + file.getName());
            } else {
                System.out.println("File already exists.");
            }

        } catch (Exception e) {
            // Handle any potential exceptions
            System.out.println("An error occurred.");
            e.printStackTrace();
        }
    }
}

Explanation:

  • File file = new File("example.txt"): Creates a File object for “example.txt”.
  • createNewFile(): Attempts to create the file. Returns ‘true’ if successful; otherwise, false.
  • Error Handling: Wrapped in a try-catch block to handle exceptions like IOException.

Suggested Read: Methods in Java

2. Reading Files in Java

Fundamental operation of reading files is possible with multiple ways provided by Java.

Method 1: Using FileReader and BufferedReader

FileReader reads characters from files, and BufferedReader helps us by buffering large chunks.

Code Example:

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

public class ReadFileExample {
    public static void main(String[] args) {
        try {
            // Open file for reading using FileReader
            FileReader fileReader = new FileReader("example.txt");

            // Wrap FileReader in BufferedReader for efficient reading
            BufferedReader bufferedReader = new BufferedReader(fileReader);

            String line;
            // Read and print each line
            while ((line = bufferedReader.readLine()) != null) {
                System.out.println(line);
            }

            // Close resources
            bufferedReader.close();

        } catch (Exception e) {
            System.out.println("An error occurred while reading the file.");
            e.printStackTrace();
        }
    }
}

Explanation:

  • BufferedReader: It is a buffer that reads input for efficiency and fewer I/Os.
  • readLine(): It reads one line at a time and returns a null upon the end of the file is reached.

Method 2: Using Scanner

The Scanner class provides a simple way to read files.

Code Example:

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

public class ScannerExample {
    public static void main(String[] args) {
        try {
            // Create Scanner object to read file
            Scanner scanner = new Scanner(new File("example.txt"));

            // Loop through each line of the file
            while (scanner.hasNextLine()) {
                System.out.println(scanner.nextLine());
            }

            // Close scanner
            scanner.close();

        } catch (Exception e) {
            System.out.println("An error occurred.");
            e.printStackTrace();
        }
    }
}

Explanation:

  • Scanner: Simplifies reading files line-by-line or token-by-token.
  • Use Case: Suitable for smaller files or basic text processing.

3. Writing to Files in Java

Writing to files is equally important and can be done using FileWriter or BufferedWriter.

Method 1: Using FileWriter

Code Example:

import java.io.FileWriter;

public class WriteFileExample {
    public static void main(String[] args) {
        try {
            // Open FileWriter for writing
            FileWriter writer = new FileWriter("example.txt");

            // Write content to the file
            writer.write("Hello, this is an example of file writing in Java!");

            // Close the writer
            writer.close();
            System.out.println("Successfully wrote to the file.");

        } catch (Exception e) {
            System.out.println("An error occurred.");
            e.printStackTrace();
        }
    }
}

Explanation:

  • FileWriter: Writes text to files. Existing content is overwritten unless specified otherwise.
  • Use Case: Best for simple writing operations.

Suggested: Learn about Access Modifiers in Java to manage your file-handling classes better.

Method 2: Using BufferedWriter

Buffered writing improves efficiency for large files.

Code Example:

import java.io.BufferedWriter;
import java.io.FileWriter;

public class BufferedWriterExample {
    public static void main(String[] args) {
        try {
            // Enable appending mode
            BufferedWriter writer = new BufferedWriter(new FileWriter("example.txt", true));

            // Append content to the file
            writer.write("Appending new content!\n");
            writer.close();

            System.out.println("Content appended successfully.");

        } catch (Exception e) {
            System.out.println("An error occurred.");
            e.printStackTrace();
        }
    }
}

Explanation:

  • BufferedWriter: Buffers data before writing, reducing disk I/O operations.
  • Appending: The second argument in FileWriter enables appending instead of overwriting.

Exception Handling in File Operations

When working with files in Java it is common to receive exceptions like IOException and FileNotFoundException because of things like missing files, permission restrictions or disk related problems. 

The exception handling mechanism of Java makes sure that such errors can be handled gracefully and not abrupt termination of the program.

Suggested Read: Exception Handling in Java with Examples

Common Exceptions in File Handling

  1. IOException: A general exception for Input/Output operations that encounter errors, such as failing to read or write to a file.
  2. FileNotFoundException: A subclass of IOException that specifically occurs when the program tries to access a file that does not exist or cannot be opened due to restricted access.
  3. SecurityException: Thrown if the application does not have the appropriate permissions to access a file.

Best Practices for Exception Handling

1. Using try-catch Blocks

A try-catch block is a standard way to manage exceptions. It allows you to attempt risky operations within the try block and catch any resulting exceptions in the catch block.

Example Code:

import java.io.File;Access Modifiers
import java.io.FileReader;

public class TryCatchExample {
    public static void main(String[] args) {
        try {
            // Attempt to open a file
            FileReader fileReader = new FileReader("nonexistentFile.txt");
            System.out.println("File opened successfully.");
        } catch (FileNotFoundException e) {
            // Handle the error gracefully
            System.out.println("Error: File not found. Please check the file path.");
        } catch (IOException e) {
            // Handle general I/O exceptions
            System.out.println("An I/O error occurred.");
        } finally {
            System.out.println("Execution completed.");
        }
    }
}

Explanation:

  1. try block: Encapsulates code that might throw exceptions, such as opening or reading a file.
  2. catch block: Catches specific exceptions (FileNotFoundException) or more general ones (IOException) and allows for error messages or alternative logic.
  3. finally block: Executes cleanup code, such as closing files, regardless of whether an exception was thrown.

2. Using try-with-resources

Introduced in Java 7, the try-with-resources statement simplifies resource management. Resources like file streams or readers are automatically closed at the end of the block, eliminating the need for explicit cleanup code.

Key Advantages:

  1. Ensures all resources are closed properly, even if an exception occurs.
  2. Reduces boilerplate code and avoids potential memory leaks.

Example Code:

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

public class TryWithResourcesExample {
    public static void main(String[] args) {
        // Automatically manage the BufferedReader resource
        try (BufferedReader br = new BufferedReader(new FileReader("example.txt"))) {
            String line;
            while ((line = br.readLine()) != null) {
                System.out.println(line);
            }
        } catch (FileNotFoundException e) {
            System.out.println("Error: File not found.");
        } catch (IOException e) {
            System.out.println("An I/O error occurred.");
        }
    }
}

Explanation:

  1. try with parentheses: Declares resources (BufferedReader in this case) that are automatically closed when the block is exited.
  2. No need for finally: Resources are closed implicitly, making the code cleaner.

Comparison: try-catch vs try-with-resources

Featuretry-catchtry-with-resources
Resource ManagementManual (requires finally block).Automatic (resources closed automatically).
Boilerplate CodeMore verbose due to explicit cleanup.Cleaner and concise.
Best ForOlder Java versions or non-closeable resources.Efficient management of closeable resources.

When to Use Which Approach?

  • Use try-catch when handling operations that don’t involve resources requiring cleanup (e.g., parsing strings or performing calculations).
  • Use try-with-resources when working with streams, readers, or other AutoCloseable resources to ensure proper cleanup with minimal code.

Frequently Asked Questions

1. What are common pitfalls to avoid in file handling?

  1. Not Closing Resources: Always close streams, readers, or channels—prefer try-with-resources to avoid leaks.
  2. Assuming a File’s Existence: Always check exists() or handle FileNotFoundException.
  3. Hardcoding Paths: Use relative paths or external config to make your code environment-agnostic.
  4. Ignoring Character Encoding: Be mindful of encodings (UTF-8, ISO-8859-1, etc.) to avoid malformed data.

2. How do I handle binary files (images, PDFs) in Java?

  • For binary data, you’d use InputStream / OutputStream (e.g., FileInputStream, FileOutputStream) rather than Reader / Writer.
  • Read/write in byte arrays or buffers. Don’t rely on character-based methods, as binary files don’t map directly to text.

3. How does the finally block complement exception handling?

The finally block is used to execute cleanup code, such as closing file streams, regardless of whether an exception is thrown or not. This ensures resources are always released, preventing potential memory leaks or resource locks.

4. Why is exception handling considered a good practice in Java file operations?

Exception handling is considered a good practice in Java file operations because:

  • Prevents abrupt program crashes.
  • Provides meaningful feedback to users about errors.
  • Ensures resources (e.g., files, streams) are properly closed and managed.
  • Makes the code robust and maintainable, as it adheres to structured error-handling mechanisms.

Also Read: Multithreading in Java

→ Explore this Curated Program for You ←

Avatar photo
Great Learning Editorial Team
The Great Learning Editorial Staff includes a dynamic team of subject matter experts, instructors, and education professionals who combine their deep industry knowledge with innovative teaching methods. Their mission is to provide learners with the skills and insights needed to excel in their careers, whether through upskilling, reskilling, or transitioning into new fields.

Full Stack Software Development Course from UT Austin

Learn full-stack development and build modern web applications through hands-on projects. Earn a certificate from UT Austin to enhance your career in tech.

4.8 ★ Ratings

Course Duration : 28 Weeks

Cloud Computing PG Program by Great Lakes

Enroll in India's top-rated Cloud Program for comprehensive learning. Earn a prestigious certificate and become proficient in 120+ cloud services. Access live mentorship and dedicated career support.

4.62 ★ (2,760 Ratings)

Course Duration : 8 months

Scroll to Top