Explain File Handling in Java with example

File Handling in Java

File handling in Java is an essential concept that allows programs to read from and write to files stored on the file system. Java provides various classes and methods to handle files through the java.io and java.nio.file packages. Here, we’ll focus on the java.io package to illustrate basic file handling operations such as creating, writing, reading, and deleting files.

File Handling in Java: Basic Operations

  • Creating a File
  • Writing to a File
  • Reading from a File
  • Deleting a File

Creating a File

To create a file, you can use the File class from the java.io package. The createNewFile method is used to create a new, empty file if it does not already exist.

import java.io.File;
import java.io.IOException;

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

Writing to a File

To write to a file, you can use the FileWriter class. It provides methods to write text data to files.

import java.io.FileWriter;
import java.io.IOException;

public class WriteFileExample {
    public static void main(String[] args) {
        try {
            FileWriter writer = new FileWriter("example.txt");
            writer.write("Hello, this is a text written to the file.");
            writer.close();
            System.out.println("Successfully wrote to the file.");
        } catch (IOException e) {
            System.out.println("An error occurred.");
            e.printStackTrace();
        }
    }
}

Reading from a File

To read from a file, you can use the FileReader and BufferedReader classes. They provide methods to read text data from files.

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

public class ReadFileExample {
    public static void main(String[] args) {
        try {
            FileReader reader = new FileReader("example.txt");
            BufferedReader bufferedReader = new BufferedReader(reader);
            
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                System.out.println(line);
            }
            reader.close();
        } catch (IOException e) {
            System.out.println("An error occurred.");
            e.printStackTrace();
        }
    }
}

Deleting a File

To delete a file, you can use the delete method of the File class.

import java.io.File;

public class DeleteFileExample {
    public static void main(String[] args) {
        File file = new File("example.txt");
        if (file.delete()) {
            System.out.println("Deleted the file: " + file.getName());
        } else {
            System.out.println("Failed to delete the file.");
        }
    }
}

Summary

These examples illustrate basic file handling operations in Java using the java.io package. For more advanced file operations, such as working with paths, directories, and more complex I/O operations, you might consider using the java.nio.file package, which provides more modern and comprehensive file I/O capabilities.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *