Tutorial

Java get file size

Published on August 3, 2022
Default avatar

By Pankaj

Java get file size

While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

Today we will look into different ways to get file size in Java.

Java get file size

There are different classes that we can use for java get file size program. Some of them are;

  1. Java get file size using File class
  2. Get file size in java using FileChannel class
  3. Java get file size using Apache Commons IO FileUtils class

Before we look into an example program to get file size, we have a sample pdf file with size 2969575 bytes. java file size, java get file size

Java get file size using File class

Java File length() method returns the file size in bytes. The return value is unspecified if this file denotes a directory. So before calling this method to get file size in java, make sure file exists and it’s not a directory. Below is a simple java get file size example program using File class.

package com.journaldev.getfilesize;

import java.io.File;

public class JavaGetFileSize {

	static final String FILE_NAME = "/Users/pankaj/Downloads/file.pdf";

	public static void main(String[] args) {
		File file = new File(FILE_NAME);
		if (!file.exists() || !file.isFile()) return;

		System.out.println(getFileSizeBytes(file));
		System.out.println(getFileSizeKiloBytes(file));
		System.out.println(getFileSizeMegaBytes(file));
	}

	private static String getFileSizeMegaBytes(File file) {
		return (double) file.length() / (1024 * 1024) + " mb";
	}
	
	private static String getFileSizeKiloBytes(File file) {
		return (double) file.length() / 1024 + "  kb";
	}

	private static String getFileSizeBytes(File file) {
		return file.length() + " bytes";
	}
}

java get file size

Get file size in java using FileChannel class

We can use FileChannel size() method to get file size in bytes.

package com.journaldev.getfilesize;

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

public class JavaGetFileSizeUsingFileChannel {

	static final String FILE_NAME = "/Users/pankaj/Downloads/file.pdf";

	public static void main(String[] args) {

		Path filePath = Paths.get(FILE_NAME);
		FileChannel fileChannel;
		try {
			fileChannel = FileChannel.open(filePath);
			long fileSize = fileChannel.size();
			System.out.println(fileSize + " bytes");
			fileChannel.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

Java get file size using Apache Commons IO FileUtils class

If you are already using Apache Commons IO in your project, then you can use FileUtils sizeOf method to get file size in java.

package com.journaldev.getfilesize;

import java.io.File;

import org.apache.commons.io.FileUtils;

public class JavaGetFileSizeUsingApacheCommonsIO {

	static final String FILE_NAME = "/Users/pankaj/Downloads/file.pdf";

	public static void main(String[] args) {
		File file = new File(FILE_NAME);

		long fileSize = FileUtils.sizeOf(file);

		System.out.println(fileSize + " bytes");
	}
}

That’s all for java get file size programs.

You can checkout more Java IO examples from our GitHub Repository.

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about us


About the authors
Default avatar
Pankaj

author

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
JournalDev
DigitalOcean Employee
DigitalOcean Employee badge
November 16, 2021

Thanks for the code, helped me in my assignment

- Mukta

    JournalDev
    DigitalOcean Employee
    DigitalOcean Employee badge
    April 5, 2019

    About File vs FileUtils vs FileChannel: Is there any reason to choose one above the other or is it just that people choose whatever they like?

    - Arjan

      JournalDev
      DigitalOcean Employee
      DigitalOcean Employee badge
      March 7, 2018

      Iam sending pdf filename to java through ajax call.While saving that filename in a folder it showing only filename content is not displaying.

      - test1234

        Try DigitalOcean for free

        Click below to sign up and get $200 of credit to try our products over 60 days!

        Sign up

        Join the Tech Talk
        Success! Thank you! Please check your email for further details.

        Please complete your information!

        Get our biweekly newsletter

        Sign up for Infrastructure as a Newsletter.

        Hollie's Hub for Good

        Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

        Become a contributor

        Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

        Welcome to the developer cloud

        DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

        Learn more
        DigitalOcean Cloud Control Panel