Apache Commons IO to get Readable File Size in KB MB GB TB

Tags: Apache Commons IO

Java Code Examples for using byteCountToDisplaySize() method of org.apache.commons.io.FileUtils class

package simplesolution.dev;

import org.apache.commons.io.FileUtils;
import java.math.BigInteger;

public class FileUtilsByteCountToDisplaySizeExample {

    public static void main(String... args) {
        String result = FileUtils.byteCountToDisplaySize(new BigInteger("123456789"));
        System.out.println(result); // 117MB

        result = FileUtils.byteCountToDisplaySize(new BigInteger("123"));
        System.out.println(result); // 123 bytes

        result = FileUtils.byteCountToDisplaySize(new BigInteger("123456"));
        System.out.println(result); //120 KB

        result = FileUtils.byteCountToDisplaySize(new BigInteger("123456789012345"));
        System.out.println(result); //112 TB

        result = FileUtils.byteCountToDisplaySize(1234567890);
        System.out.println(result); //1 GB
    }
}

Happy Coding 😊

Apache Commons IO to get User Directory

Apache Commons IO to get Temporary Directory