All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.craftercms.commons.file.FileUtils Maven / Gradle / Ivy

There is a newer version: 4.3.1
Show newest version
package org.craftercms.commons.file;

import java.text.DecimalFormat;

/**
 * General file manipulation utilities.
 */
public class FileUtils {

    /**
     * Prevents a Instance of FileUtils.
     */
    private FileUtils(){}

    /**
     * Given the size of a file outputs as human readable size using SI prefix.
     * Base 1024
     * @param size Size in bytes of a given File.
     * @return SI String representing the file size (B,KB,MB,GB,TB).
     */
    public static String readableFileSize(long size) {
        if (size <= 0) {
            return "0";
        }
        final String[] units = new String[] {"B", "KB", "MB", "GB", "TB"};
        int digitGroups = (int)(Math.log10(size) / Math.log10(1024));
        return new DecimalFormat("#,##0.#").format(size / Math.pow(1024, digitGroups)) + " " + units[digitGroups];
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy