In Java, we can use Files.size(path) to get the size of a file in bytes.

  // size in bytes
  long bytes = Files.size(path);

1. Files.size (NIO)

This example uses NIO Files.size(path) to print the size of an image file (140 kb).

GetFileSize.java

package com.favtuts.io.howto;

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

public class GetFileSize {
    
    public static void main(String[] args) {
        // this image is around 140kb
        String fileName = "/home/tvt/Pictures/EOM.png";
        printFileSizeNIO(fileName);
    }

    public static void printFileSizeNIO(String fileName) {

        Path path = Paths.get(fileName);

        try {

            // size of a file (in bytes)
            long bytes = Files.size(path);
            System.out.println(String.format("%,d bytes", bytes));
            System.out.println(String.format("%,d kilobytes", bytes / 1024));

        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

Output

143,041 bytes
139 kilobytes

2. File.length (Legacy IO)

In the old days (Before Java 7), we can use the legacy IO File.length() to get the size of a file in bytes. This example will format the bytes up to the yottabytes, just for fun.

GetFileSize2.java

package com.favtuts.io.howto;

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

public class GetFileSize {
    
    public static void main(String[] args) {
        // this image is around 140kb
        String fileName = "/home/tvt/Pictures/EOM.png";
        printFileSize(fileName);
    }

    public static void printFileSize(String fileName) {

        File file = new File(fileName);

        if (file.exists()) {

            // size of a file (in bytes)
            long bytes = file.length();

            long kilobytes = (bytes / 1024);
            long megabytes = (kilobytes / 1024);
            long gigabytes = (megabytes / 1024);
            long terabytes = (gigabytes / 1024);
            long petabytes = (terabytes / 1024);
            long exabytes = (petabytes / 1024);
            long zettabytes = (exabytes / 1024);
            long yottabytes = (zettabytes / 1024);

            System.out.println(String.format("%,d bytes", bytes));
            System.out.println(String.format("%,d kilobytes", kilobytes));
            System.out.println(String.format("%,d megabytes", megabytes));
            System.out.println(String.format("%,d gigabytes", gigabytes));
            System.out.println(String.format("%,d terabytes", terabytes));
            System.out.println(String.format("%,d petabytes", petabytes));
            System.out.println(String.format("%,d exabytes", exabytes));
            System.out.println(String.format("%,d zettabytes", zettabytes));
            System.out.println(String.format("%,d yottabytes", yottabytes));

        } else {
            System.out.println("File does not exist!");
        }

    }
}

Output

143,041 bytes
139 kilobytes
0 megabytes
0 gigabytes
0 terabytes
0 petabytes
0 exabytes
0 zettabytes
0 yottabytes

As the time of writing, the disk storage is still in terabytes, is petabytes hard disk or SSD exists?

Download Source Code

$ git clone https://github.com/favtuts/java-core-tutorials-examples

$ cd java-io/howto

References

Leave a Reply

Your email address will not be published. Required fields are marked *