Open In App

How to Get the Size of a File in Java?

Last Updated : 07 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In Java, file transferring, retrieving content of files, and manipulation of files developers frequently require the size of the file to perform proper file handling. In Java, there are many ways to get the size of the file.

In this article, we will explore the most commonly used approaches to get the size of the file in Java.

Program to Get the Size of a File in Java

1. Using File Class

Let’s explore how to get the file size using File Class.

Java




// Java program to get the size of a file using File class
import java.io.File;
  
class MyClass {
        //Storing path file in a String  
        static final String PATH = "C:\\Users\\Dhanush\\frontend.txt";  
        public static void main(String args[])   
        
              
            //specifying the file path to the File constructor  
            File file = new File(PATH);  
          
            //checking if the file exists or the file specified is of the type file    
            if ((file.exists()) && (file.isFile()))    
            {
                //displays the file size in bytes  
                System.out.println("The File Size in bytes : "+convertToBytes(file)+" bytes");  
              
                //displays the file size in KB  
                System.out.println("The File Size in KiloBytes : "+convertToKilobytes(file)+"kb");  
                  
                //displays the  file size in MB  
                System.out.println("The File Size in MegaBytes : "+convertToMegabytes(file)+"mb");  
            }  
               else {
                  System.out.println("OOPs! The Specified File not found.");
                }
        }
        // Function which calculates the file size in megabytes  
        private static long convertToMegabytes(File file)   
        {  
            return  file.length() / (1024 * 1024);  
        }  
          
          
        // Function which calculates the file size in kilobytes  
        private static long convertToKilobytes(File file)   
        {  
            return file.length() / 1024;  
        }  
          
        //Function which calculates the file size in bytes  
        private static long convertToBytes(File file)   
        {  
            return  file.length();
        }  
}


Output:

Output in Console

Explanation of the Program:

  • In this approach, implemented the .length() function to get the size of the file.
  • Firstly it checks if the file exists or the file specified is of the type file.
  • Then by using .length() function the length of the file with respective size type(mb,kb and bytes) the size will be displayed.

2. Using FileChannel Class

Let’s explore how to get the file size using FileChannel Class.

Java




// Java program to get the size of a file using FileChannel class
import java.io.File;
import java.io.IOException;
import java.nio.channels.FileChannel;
import java.nio.file.StandardOpenOption;
  
public class MyClass {
    
    // path to the file
    static final String PATH = "C:\\Users\\Dhanush\\frontend.txt";
  
    public static void main(String args[]) throws IOException {
        // create a File object with the specified path
        File file = new File(PATH);
  
        // check if the file exists and is a regular file
        if (file.exists() && file.isFile()) {
            // Try-with-resources to ensure the FileChannel is properly closed
            try (FileChannel channel = FileChannel.open(file.toPath(), StandardOpenOption.READ)) {
                // get the size of the file
                long fileSize = channel.size();
                // display the file size in bytes
                System.out.println("File size : " + fileSize + " bytes");
            }
        } else {
            // display an error message if the file is not found
            System.out.println("OOPs! The specified file not found.");
        }
    }
}


Output:

Output in Console2

Explanation of the Program:

  • In this approach, implemented the FileChannel class to get the size of the file.
  • Firstly, it checks if the file exists, or the file specified is of the type of file.
  • By using the .size() function the size of the file will be displayed in bytes by default.

Note: Both the approaches have been written and executed in eclipse IDE.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads