How to Get the Size of a File in Java? Last Updated : 07 Feb, 2024 Comments Improve Suggest changes Like Article Like 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 Java1. Using File ClassLet'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: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 ClassLet'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: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. Comment More infoAdvertise with us Next Article How to Get the Size of a File in Java? D dhanush77 Follow Improve Article Tags : Java Java Programs java-file-handling Java Examples Practice Tags : Java Similar Reads How to Get a Size of Collection in Java? Given a Collection in Java, the task is to find the length or size of the collection. Examples: Input: Array_List: [1, 2, 3,4] Output: 4 Input: Linked_List: [geeks, for, geeks] Output: 3 The Size of the different collections can be found with the size() method. This method returns the number of elem 2 min read How to Get the File's Parent Directory in Java? Java is one of the most popular and powerful programming languages. It is widely used for various purposes. In this article, we are going to learn how to find the parent directory of a file or a specified path. Methods to Get the Parent Directory of a fileIn Java, we can get the parent Directory wit 3 min read Java Program to Get the Size of a Directory The size of files in Java can be obtained using the File class. The in-built function of 'fileName.length()' is used to find size of file in Bytes. The directory may contain 'N' number of files, for calculating the size of the directory summation of the size of all the files is required. length() Me 2 min read How to Read a File From the Classpath in Java? Reading a file from the classpath concept in Java is important for Java Developers to understand. It is important because it plays a crucial role in the context of Resource Loading within applications. In Java, we can read a file from the classpath by using the Input Stream class. By loading the cla 3 min read Java Program to Get the Size of Given File in Bytes, KiloBytes and MegaBytes The in-built File.length() method in java can be used to get file size in java. The length() function is a part of the File class in Java. This function returns the length of the file whose path was specified. If the file does not exist or some error happens then it returns 0. Parameters: This metho 2 min read Like