File Handling in Java

Last Updated : 13 Mar, 2026

In Java, file handling means working with files like creating them, reading data, writing data or deleting them. It helps a program save and use information permanently on the computer.

file_handling_in_java_
File-operations

Why File Handling is Required?

  • To store data permanently instead of keeping it only in memory.
  • To read and write data from/to files for later use.
  • To share data between different programs or systems.
  • To organize and manage large data efficiently.

To support file handling, Java provides the File class in the java.io package.

File Class

File class in Java (from the java.io package) is used to represent the name and path of a file or directory. It provides methods to create, delete, and get information about files and directories.

Java
// Importing File Class
import java.io.File;

class Geeks{
    
    public static void main(String[] args){
        
        // File name specified
        File obj = new File("myfile.txt");
        System.out.println("File Created!");
    }
}

Output:

File Created!

In Java, I/O streams are used to perform input and output operations on files. So, let’s first understand what streams are.

I/O Streams in Java

In Java, I/O streams are the fundamental mechanism for handling input and output operations. They provide a uniform way to read data from various sources (files, network, memory) and write data to different destinations.

Java I/O streams are categorized into two main types based on the type of data they handle:

1. Byte Streams

In Java, Byte Streams are used to handle raw binary data such as images, audio files, videos or any non-text file. They work with data in the form of 8-bit bytes.

byte_streams_
Byte Streams

The two main abstract classes for byte streams are:

Since abstract classes cannot be used directly, we use their implementation classes to perform actual I/O operations.

2. Character Streams

In Java, Character Streams are used to handle text data. They work with 16-bit Unicode characters, making them suitable for international text and language support.

character_streams
Character Stream

The two main abstract classes for character streams are:

  • Reader: Base class for all character-based input streams (reading).
  • Writer: Base class for all character-based output streams (writing).

Since abstract classes cannot be used directly, we use their implementation classes to perform actual I/O operations.

Use Byte Streams when working with binary data (images, audio, video, executable files) and use Character Streams when working with text data (characters, strings, text files).

File Operations

The following are the several operations that can be performed on a file in Java:

1. Create a File

  • In order to create a file in Java, you can use the createNewFile() method.
  • If the file is successfully created, it will return a Boolean value true and false if the file already exists.
Java
import java.io.File;
import java.io.IOException;

public class CreateFile
{
    public static void main(String[] args)
    {

        try {
            File Obj = new File("myfile.txt");
            
          	// Creating File
          	if (Obj.createNewFile()) {
                System.out.println("File created: " + Obj.getName());
            }
            else {
                System.out.println("File already exists.");
            }
        }
      
      	// Exception Thrown
        catch (IOException e) {
            System.out.println("An error has occurred.");
            e.printStackTrace();
        }
    }
}

Output:

CreateFile
Output

2. Write to a File

We use the FileWriter class along with its write() method in order to write some text to the file.

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

public class WriteFile 
{
    public static void main(String[] args)
    {
        // Writing Text File       
        try {

            FileWriter Writer = new FileWriter("myfile.txt");

            // Writing File
            Writer.write("Files in Java are seriously good!!");
            Writer.close();
            
            System.out.println("Successfully written.");
        }

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

Output:

Writing
Output

3. Read from a File

In Java, the read() method is used with classes like FileReader or InputStream to read data from a file one character or byte at a time.

  • It returns an integer value representing the character or byte read.
  • When the end of the file is reached, the method returns -1 indicating no more data is available.
Java
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner; 

public class ReadFile 
{
    public static void main(String[] args)
    {
        // Reading File
        try {
            File Obj = new File("myfile.txt");
            Scanner Reader = new Scanner(Obj);
          
            // Traversing File Data
          	while (Reader.hasNextLine()) {
                String data = Reader.nextLine();
                System.out.println(data);
            }
          
            Reader.close();
        }
        
        // Exception Cases
        catch (FileNotFoundException e) {
            System.out.println("An error has occurred.");
            e.printStackTrace();
        }
    }
}

Output:

ReadFile
Output

4. canRead a File

canRead() check if the file is readable.

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

public class CanReadExample {
    public static void main(String[] args) {
        try {
            // Create a new file
            File obj = new File("myfile.txt");

            if (obj.createNewFile()) {
                System.out.println("File created: " + obj.getName());
            } else {
                System.out.println("File already exists.");
            }

            // Check if the file is readable
            if (obj.canRead()) {
                System.out.println("The file is readable.");
            } else {
                System.out.println("The file is not readable.");
            }

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

Output:

out
output

5. canWrite a File

canWrite() checks whether the file can be written to by the program.

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

public class CanWriteExample {
    public static void main(String[] args) {
        try {
            // Create a new file
            File obj = new File("myfile.txt");

            if (obj.createNewFile()) {
                System.out.println("File created: " + obj.getName());
            } else {
                System.out.println("File already exists.");
            }

            // Check if the file is writable
            if (obj.canWrite()) {
                System.out.println("The file is writable.");  // One-line explanation
            } else {
                System.out.println("The file is not writable.");
            }

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

Output:

out2
Output

6. Existance of a File

exists() checks whether the specified file or directory exists on the file system.

Java
import java.io.File;

public class ExistsExample {
    public static void main(String[] args) {
        // Create a File object
        File obj = new File("myfile.txt");

        // Check if the file exists
        if (obj.exists()) {
            System.out.println("The file exists.");  // One-line explanation
        } else {
            System.out.println("The file does not exist.");
        }
    }
}

Output:

Out3
Output

7. Getting a path

getAbsolutePath() returns the full path of the file or directory in the file system.

Java
import java.io.File;

public class AbsolutePathExample {
    public static void main(String[] args) {
        // Create a File object
        File obj = new File("myfile.txt");

        // Print the absolute path of the file
        System.out.println("Absolute Path: " + obj.getAbsolutePath());
    }
}
out4
Output

8. Delete a File

We use the delete() method in order to delete a file.

Java
import java.io.File; 

public class DeleteFile 
{
    public static void main(String[] args)
    {
        File Obj = new File("myfile.txt");
        
        // Deleting File
        if (Obj.delete()) {
            System.out.println("The deleted file is : " + Obj.getName());
        }
        else {
            System.out.println(
                "Failed in deleting the file.");
        }
    }
}

Output:

DeleteFile
Output
Comment