File Handling in Java Explained with Create Read Write Modify Examples

File Handling in Java banner showing create read write modify operations with code examples and file system concept

File handling is an essential concept in Java that allows you to store data permanently. Whether you are building a real-world application or preparing for exams, understanding how to create, read, write, and modify files is extremely important.

In this guide, you’ll learn file handling in Java step by step with simple examples that are easy to understand.


What is File Handling in Java?

File handling refers to performing operations on files such as creating, reading, writing, and updating data stored in files.

Java provides built-in classes from the java.io package to handle files.


Common File Handling Classes

  • File → represents file or directory
  • FileWriter → writes data to file
  • FileReader → reads data from file
  • BufferedReader → reads text efficiently

1. Create a File in Java

Let’s start by creating a file.

Create File Example
import java.io.File;
import java.io.IOException;

public class CreateFile {
    public static void main(String[] args) {
        try {
            File file = new File("example.txt");
            if (file.createNewFile()) {
                System.out.println("File created: " + file.getName());
            } else {
                System.out.println("File already exists");
            }
        } catch (IOException e) {
            System.out.println("Error occurred");
        }
    }
}
  

2. Write Data to File

Now let’s write some data into the file.

Write to File
import java.io.FileWriter;
import java.io.IOException;

public class WriteFile {
    public static void main(String[] args) {
        try {
            FileWriter writer = new FileWriter("example.txt");
            writer.write("Hello, this is Java file handling!");
            writer.close();
            System.out.println("Data written successfully");
        } catch (IOException e) {
            System.out.println("Error writing file");
        }
    }
}
  

3. Read Data from File

To read data, we use FileReader and BufferedReader.

Read File Example
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

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

4. Modify File in Java

Java does not directly support modifying specific parts of a file. Instead, we rewrite the file.

Modify File Example
import java.io.FileWriter;
import java.io.IOException;

public class ModifyFile {
    public static void main(String[] args) {
        try {
            FileWriter writer = new FileWriter("example.txt");
            writer.write("File content updated successfully!");
            writer.close();
            System.out.println("File modified");
        } catch (IOException e) {
            System.out.println("Error modifying file");
        }
    }
}
  

Output Example

Output
File created: example.txt
Data written successfully
Hello, this is Java file handling!
File modified
  

How File Handling Works

  • Create file using File class
  • Write data using FileWriter
  • Read data using FileReader/BufferedReader
  • Modify by rewriting file

Real-World Applications

  • Saving user data
  • Logging system activity
  • Configuration files
  • Report generation

Common Mistakes

  • Forgetting to close file
  • Not handling exceptions
  • Overwriting data unintentionally

Best Practices

  • Always close files
  • Use try-with-resources (advanced)
  • Handle exceptions properly

FAQ

What is file handling in Java?

It is the process of creating, reading, writing, and updating files.

Which package is used?

The java.io package is used.

How to modify a file?

By rewriting its content using FileWriter.


Conclusion

File handling is a fundamental skill in Java programming. By understanding how to create, read, write, and modify files, you can build real-world applications that store and manage data effectively.

Practice these examples to strengthen your understanding and improve your Java skills.


📚 Read More

Comments