Java Program to Get the File Size

In this article you will see how you can get the size of file by using Java programming language.

Java Program to Get the File Size

Every file which is present in your system occupies some memory space to be stored in the system. File size tells about the amount of memory required for storing that file or in simple the file size is the amount of space taken by the file in the memory.

Let’s see program to understand it more clearly.

Method-1: Java Program to Get the File Size by Using File Class length() Method

In Java we have File class in java.io package. File class provides an inbuilt length() method to get the size of the specified file in bytes.

Approach:

  • Declare a String variable and assign the file path for which you want to know the file size.
  • Create an object of File class by passing file path as parameter.
  • Then check specified path or file is actually a file or not by using isFile() method and it is available in the specifies path or not by using exists() method. If that is not a normal file or not available in path then simply return.
  • Else get the size of file by using inbuilt length() method.

Program:

import java.io.File;

public class Main
{
    public static void main(String[] args) 
    {
        //declare a String variable filePath and assign the path of file 
        //for which file you want to know the size
        String filePath = "F:\\Tutorial IoT\\SDN Cisco PPT.pdf";
        //Create a object of File class and pass that filePath as parameter
        File file = new File(filePath);
        
        //Check if file does not exist by using inbuilt exists() method
        //or check if file is not a normal file type by using inbuilt isFile() method
        //then return 
        if (!file.exists() || !file.isFile()) 
            return;
        
        //else get the file size by using length() method of File class
        System.out.println("Your File size is: ");
        System.out.println("In Bytes: "+file.length() + " bytes");
        System.out.println("In KiloBytes: "+ file.length() / 1024 + "  kb");
        System.out.println("In MegaBytes: "+file.length() / (1024 * 1024) + " mb");
    }
}

Output:

Your File size is: 
In Bytes: 11261028 bytes
In KiloBytes: 10997 kb
In MegaBytes: 10 mb

Method-2: Java Program to Get the File Size by Using FileChannel Class size() Method

In Java we have FileChannel class in java.nio.channels package. FileChannel class provides an inbuilt size() method to get the size of the specified file in bytes.

Approach:

  • Declare a String variable and assign the file path for which you want to know the file size.
  • Create an object of Path class by passing file path as parameter.
  • Then create an object of FileChannel class by passing object of Path class as parameter.
  • Then get the size of file by using inbuilt size() method.

Program:

import java.io.IOException;
import java.nio.channels.FileChannel;
import java.nio.file.Path;
import java.nio.file.Paths;

public class Main
{
    public static void main(String[] args) throws IOException 
    {
        //declare a String variable filePath and assign the path of file 
        //for which file you want to know the size
        String filePath = "F:\\Tutorial IoT\\SDN Cisco PPT.pdf";
        //Create a object of Path class and pass that filePath as parameter
        Path path = Paths.get(filePath);

        //Create an object of FileChannel class and pass the path as parameter
        //Inbuilt open() method returns a filechannel to access the file
        FileChannel fileChannel = FileChannel.open(path);
        //Get the size by using inbuilt size() method of FileChannel class
        long fileSize = fileChannel.size();
        System.out.println("Your file size: "+fileSize + " bytes");
        //Closing the filechannel
        fileChannel.close();
    }
}

Output:

Your file size: 11261028 bytes

Method-3: Java Program to Get the File Size by Using Files Class size() Method

In Java we have Files class in java.nio.file package. Files class provides an inbuilt size() method to get the size of the specified file in bytes.

Approach:

  • Declare a String variable and assign the file path for which you want to know the file size.
  • Create an object of Path class by passing file path as parameter.
  • Then create an object of Files class by passing object of Path class as parameter.
  • Then get the size of file by using inbuilt size() method.

Program:

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class Main
{
    public static void main(String[] args) throws IOException 
    {
        //declare a String variable filePath and assign the path of file 
        //for which file you want to know the size
        String filePath = "F:\\Tutorial IoT\\SDN Cisco PPT.pdf";
        //Create a object of Path class and pass that filePath as parameter
        Path path = Paths.get(filePath);
        
        //Get the file size by using inbuilt size() method of File class
        long fileSize = Files.size(path);
        //print the file size
        System.out.println("Your file size: "+fileSize + " bytes");

    }
}

Output:

Your file size: 11261028 bytes

Have you mastered basic programming topics of java and looking forward to mastering advanced topics in a java programming language? Go with these ultimate Advanced java programs examples with output & achieve your goal in improving java coding skills.