Java Utililty Methods File Size Get

List of utility methods to do File Size Get

Description

The list of methods to do File Size Get are organized into topic(s).

Method

longgetFileSizes(File f)
get File Sizes
long s = 0;
if (f.exists()) {
    FileInputStream fis = null;
    fis = new FileInputStream(f);
    s = fis.available();
return s;
StringgetFileSizeString(File file, boolean si)
Get the formatted File size as a String
See http://stackoverflow.com/questions/3758606/ for more information on how to properly format the size
if (!file.isDirectory()) {
    long bytes = file.length();
    int unit = si ? 1000 : 1024;
    if (bytes < unit)
        return bytes + " B";
    int exp = (int) (Math.log(bytes) / Math.log(unit));
    String pre = (si ? "kMGTPE" : "KMGTPE").charAt(exp - 1) + (si ? "" : "i");
    return String.format("%.1f %sB", bytes / Math.pow(unit, exp), pre);
...
StringgetFilesizeString(long size)
get Filesize String
String tail = "";
if (1024 > size) {
    tail = "byte";
} else if (1048576 > size) {
    size = size / 1024;
    tail = "Kb";
} else if (1073741824 > size) {
    size = size / 1024;
...
StringgetFileSizeStringFor(File file)
Returns a string representation of a file size, such as "842 bytes", "1.43 KB" or "3.4 MB".
return getFileSizeStringFor(file.length(), false);
StringgetFolderSize(File folder)
get Folder Size
if (folder == null)
    return "";
if (folder.isDirectory()) {
    return getFilesSize(folder.listFiles());
} else {
    return "";
StringgetSize(long fileSize)
get Size
String size = getSize(fileSize, 2);
return size;
StringgetSizeErrorMessage(String pattern, Long maxFileSize, Long fileSize, String fileName)
get Size Error Message
return MessageFormat.format(pattern, getHumanReadableByteCount(maxFileSize, false),
        getHumanReadableByteCount(fileSize, false), fileName);
StringgetStringSizeLengthFile(long size)
get String Size Length File
DecimalFormat df = new DecimalFormat("0.00");
float sizeKb = 1024.0f;
float sizeMo = sizeKb * sizeKb;
float sizeGo = sizeMo * sizeKb;
float sizeTerra = sizeGo * sizeKb;
if (size < sizeMo)
    return df.format(size / sizeKb) + " Kb";
else if (size < sizeGo)
...
StringhumanFileSize(Long longFileSize)
Converts the size of a file to a human-readable string, e.g.
if (longFileSize < 0)
    return ("");
if (longFileSize < 512)
    return (longFileSize.toString());
double doubleBytes;
DecimalFormat outputFormat = new DecimalFormat("0.00");
StringBuffer outputStrBuf = new StringBuffer();
FieldPosition fieldPos = new FieldPosition(0);
...
StringhumanReadableFileSize(long size)
human Readable File Size
if (size <= 0) {
    return "0";
final String[] units = new String[] { "B", "KB", "MB", "GB", "TB" };
int magnitude = (int) (Math.log10(size) / Math.log10(1024));
return new DecimalFormat("#,##0.#").format(size / Math.pow(1024, magnitude)) + " " + units[magnitude];